Marione
Marione

Reputation: 13

gameObject.GetComponent returning null

I've got a problem.

In one of my scripts when I try to get a component it return me null with this error in console: "NullReferenceException: A null value was found where an object instance was required."

I don't know why because in other files the process I use works fine.

Here my code flow: First one - GameController.cs

public class GameController : MonoBehaviour {

    private SpawningPositions spawningPositions; //IT WORKS

    private void Awake()
    {
        spawningPositions = GetComponent<SpawningPositions>(); //IT WORKS
    }

    void Start()
    {
        if (enemyWaveTypes.Length != 0 && wavesNumberEnemy.Length != 0)
        {
            StartCoroutine(SpawnShips());
        }
    }

    IEnumerator SpawnShips()
    {
        while (true)
        {
            for (int i = 0; i < wavesNumberEnemy[waveCounter]; i++)
            {
                spawningPositions.SpawnRandom(enemyShip); //IT WORKS // NEXT SCRIPT
            }
            waveCounter++;
            yield return new WaitForSeconds(waveTimingRatio);
        }
    }
}

Second One - SpawningPositions.cs

public class SpawningPositions : MonoBehaviour {

    GeoData geoData; //IT WORKS

    private void Awake()
    {
        geoData = GetComponent<GeoData>(); //IT WORKS
    }

    public void SpawnRandom(Transform enemyShip)
    {
        Vector3 spawnPosition;
        Tuple<int, float> tuple = geoData.GetRandomOuterBoundary();  //IT WORKS (i've got a custom class for Tuples)
        int randomSpawn = tuple.Item1;
        float randomPosition = tuple.Item2;
        spawnPosition = geoData.GetRandomPointIn(tuple);
        Quaternion spawnRotation = Quaternion.identity;
        var myEnemy = Instantiate(enemyShip, spawnPosition, spawnRotation);
        myEnemy.gameObject.AddComponent<FixedPointAndGo>(); // NEXT SCRIPT
    }

}

And the last one, where I've got the problem - FixedPointAndGo.cs

public class FixedPointAndGo : MonoBehaviour {

    GeoData geoData; // DOESN'T WORK!!!

    private void Awake()
    {
        geoData = gameObject.GetComponent<GeoData>();  // DOESN'T WORK!!! return NULL
    }

    private void Start()
    {       
        endPos = new Vector3(
            Random.Range(
                (geoData.horizontalInLimits.x - 2), // DOESN'T WORK!!!
                (geoData.horizontalInLimits.y - 2) // DOESN'T WORK!!!
            ),
            0,
            Random.Range(geoData.verticalInLimits.x, geoData.verticalInLimits.y) // DOESN'T WORK!!!
        );
    }
}

Why when I try to add the component GeoData in the second script works and not in the third? I cannot understand.

I tried to search solutions in this forum and in the documentation but I haven't found anything yet.

Thanks in advance

Upvotes: 1

Views: 2089

Answers (1)

Programmer
Programmer

Reputation: 125245

geoData = gameObject.GetComponent<GeoData>();  // DOESN'T WORK!!! return NULL

Notice the variable "gameObject". This refers to this GameObject this script (FixedPointAndGo) is attached to.

This means that gameObject.GetComponent<GeoData>() will return null only if the GeoData script is not attached to the GameObject your FixedPointAndGo script is attached to.

Attach GeoData to the-same GameObject your FixedPointAndGo script is attached to.


If GeoData is attached to another GameObject and you want to access it then find that GameObject first before accessing the GeoData componet attached to it.

GameObject obj = GameObject.Find("NameOfGameObjectGeoDataIsAttachedTo"); 
geoData = obj.GetComponent<GeoData>();

Finally if GeoData script is already attached to the-same GameObject FixedPointAndGo script is attached to but you are still getting null then you have mistakenly attached another FixedPointAndGo script to an empty GameObject or another GameObject which does not have the GeoData script attached to it.

Find that GameObject and remove the FixedPointAndGo script from it. You can do this by selecting your FixedPointAndGo script and then go to Assets ---> Find References In Scene and remove the script.

Upvotes: 3

Related Questions