0
\$\begingroup\$

I read the solution in this post but still not able to understand it. I have a script (AdvanceWayPoint) attached to a game object ("Cube", with tag "Cube") in my scene, and this script instantiates a prefab ("1K ohm waypoint"), and in that prefab I have attached the script WayPointPosition as shown below.

Cube setup Prefab setup

AdvanceWayPoint:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AdvanceWayPoint : MonoBehaviour
{
    public Transform player;
    public bool CreateResistor;
    int RandNum;
    public GameObject myprefab;
    public GameObject FloatingText;
    public GameObject[] Resistor;
    public GameObject[] way1;
    Vector3[] ExactResistorSpawnPoint;
    public int[] ResistorBand;
    public float[] ResistorValue;
    public float ChoosenResistorValue;
    public int Ammo;
    
    void Start()
    {
        ResistorBand = new int[36];
        ResistorValue = new float[9];
        Random.InitState(System.DateTime.Now.Millisecond);
        CreateResistor = true;
        player = new GameObject().transform;
        ExactResistorSpawnPoint = new Vector3[9];
    }

    void Update()
    {

        if (CreateResistor == true)
        {
            RandNum = 1;
            CreateResistor = false;
            Waypoint();
        }
    }

    public void Waypoint()
    {
        Vector3 SpawnPosition = player.transform.position;
        Vector3 ExactSpawnPoint = new Vector3(SpawnPosition.x + Random.Range(1, 4), 0, SpawnPosition.z + Random.Range(1, 4));
         

        if (RandNum == 1)
        {
            int i = 0;
            int y = 0;
            while (i < 35)
            {
                ExactResistorSpawnPoint[y] = new Vector3(ExactSpawnPoint.x + Random.Range(1, 10), ExactSpawnPoint.y + 7, ExactSpawnPoint.z + Random.Range(1, 10));
                ResistorBand[i] = Random.Range(1, 10);
                ResistorBand[i+1] = Random.Range(1, 10);
                while(ResistorBand[i+1]==ResistorBand[i])
                {
                    ResistorBand[i + 1] = Random.Range(1, 10);
                }
                ResistorBand[i+2] = Random.Range(1, 5);
                while(ResistorBand[i + 2]==ResistorBand[i+1]||ResistorBand[i+2]==ResistorBand[i])
                {
                    ResistorBand[i + 2] = Random.Range(1,5);
                }
                ResistorBand[i + 3] = Random.Range(1, 3);
                ResistorValue[y] = ((ResistorBand[i] * 10 + ResistorBand[i+1]) * Mathf.Pow(10, ResistorBand[i+2]) / 1000);
                i = i + 4;
                y = y + 1;                     
            }            
            way1 = GameObject.FindGameObjectsWithTag("1K ohm");
            if (way1.Length == 0)
            {
                for (y = 0; y < 9; y++)
                {                   
                    Instantiate(Resistor[y], ExactResistorSpawnPoint[y], Quaternion.identity);                                  
                }
            }
            int RandomChooseWayPointValue = Random.Range(0, 9);
            ChoosenResistorValue = ResistorValue[RandomChooseWayPointValue];
            Instantiate(myprefab, ExactSpawnPoint, Quaternion.identity);
            var go = Instantiate(FloatingText, ExactSpawnPoint, Quaternion.identity);
            go.GetComponent<TextMesh>().text = ResistorValue[RandomChooseWayPointValue].ToString() + " K ohm +/-" + ResistorBand[((RandomChooseWayPointValue+1)*4)-1].ToString() + "%";
        }
    }
}

WayPointPosition:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WayPointPosition : MonoBehaviour
{
    public GameObject FloatingText;
  
    public float ResistorValue;
    float t;
    private AdvanceWayPoint Ammo;

   

    void Awake()
    {
        GameObject ReadValue = GameObject.FindGameObjectWithTag("Cube");
        AdvanceWayPoint read = ReadValue.AddComponent<AdvanceWayPoint>();
        ResistorValue = read.ChoosenResistorValue;
        Debug.Log(ResistorValue);
        transform.rotation = Quaternion.Euler(-90, 0, 0);
        
    }

    // Update is called once per frame
    void Update()
    {
        t=t+ 5*Time.deltaTime;
        transform.position =new Vector3(transform.position.x, 2+Mathf.Abs(Mathf.Cos(t)), transform.position.z);
        
    }
    void OnTriggerEnter(Collider other)
    {
        float ThisResistorValue = other.gameObject.GetComponent<ReadResistorValue>().value;
        if (ThisResistorValue == ResistorValue)
        {
            Destroy(other.gameObject);
            AdvanceWayPoint ammo = FindObjectOfType<AdvanceWayPoint>();
            ammo.Ammo++;
           
            GameObject[] Resistor = GameObject.FindGameObjectsWithTag("Resistor");
            foreach(GameObject resistor in Resistor)
            {
                Destroy(resistor);
            }
            ammo.CreateResistor = true;
            Destroy(gameObject);
            
        }

    }
}

At runtime, the error points to the line:

Instantiate(myprefab, ExactSpawnPoint, Quaternion.identity);

and deleting these lines

GameObject ReadValue = GameObject.FindGameObjectWithTag("Cube");
AdvanceWayPoint read = ReadValue.AddComponent<AdvanceWayPoint>();
ResistorValue = read.ChoosenResistorValue; 

will no longer show the error.

On instantiate, I need the prefab to retrieve value from the Cube game object in the scene, but I'm not sure why it is not working.

\$\endgroup\$
2
  • \$\begingroup\$ just to be sure, your cube is tagged as cube and not just named cube, right? Cant be seen from your screenshot \$\endgroup\$
    – Zibelas
    Apr 4, 2022 at 12:46
  • \$\begingroup\$ Yes the cube is tagged as cube \$\endgroup\$
    – chuackt
    Apr 4, 2022 at 13:03

1 Answer 1

2
\$\begingroup\$

This line:

AdvanceWayPoint read = ReadValue.AddComponent<AdvanceWayPoint>();

adds a new AdvanceWayPoint component to the ReadValue cube. This new component is brand new, fresh from the factory. It has NO Inspector configuration settings applied to it, because it didn't exist when you were configuring this object in the Inspector. So its myprefab variable is null.

When this new component's Waypoint() method is called, it will try to instantiate this null prefab and throw the error.

It looks to me like you don't want to create a new AdvanceWayPoint component at all. You probably want to read data from the existing AdvanceWayPoint component that the cube already has attached to it. To do that, you'd write this instead:

AdvanceWayPoint read = ReadValue.GetComponent<AdvanceWayPoint>();

Notice "Get" instead of "Add" - we're fetching a reference to an existing component, not adding a new one.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .