NullReferenceException on a copy of working code

So I’ve got this piece of code which manages (or is supposed to I guess) 4 arrays of game objects. The problem is really on all lines of the SpawnAsteroid method but the if statement. All of them throw the same error when separated. I have no idea what could be causing this, especially since I have another method in this very same script that has nothing but the same code as the problematic function (in a slightly different manner) and works without problems:

The problematic code:

public class AsteroidInstatiator : MonoBehaviour {
    public int LargestAsteroidArraySize = 2;
    public GameObject Asteroid1;
    public GameObject Asteroid2;
    public GameObject Asteroid3;
    public GameObject Asteroid4;
    public Vector2 ArrayPosition;
    public Rigidbody2D Player;

    private Rigidbody2D[] Asteroid1Array;
    private Rigidbody2D[] Asteroid2Array;
    private Rigidbody2D[] Asteroid3Array;
    private Rigidbody2D[] Asteroid4Array;
    private int[] iteratorArray;

    void Start()
    {
        iteratorArray = new int[4];

        Asteroid1Array = new Rigidbody2D[LargestAsteroidArraySize];
        Asteroid2Array = new Rigidbody2D[LargestAsteroidArraySize * 2];
        Asteroid3Array = new Rigidbody2D[LargestAsteroidArraySize * 4];
        Asteroid4Array = new Rigidbody2D[LargestAsteroidArraySize * 8];

        for (int i = 0; i < LargestAsteroidArraySize; i++)
        {
            Asteroid1Array[i] = GameObject.Instantiate(Asteroid1, ArrayPosition, Quaternion.identity).GetComponent<Rigidbody2D>();
            Asteroid1Array[i].gameObject.SetActive(false);
        }

        for (int i = 0; i < LargestAsteroidArraySize * 2; i++)
        {
            Asteroid2Array[i] = GameObject.Instantiate(Asteroid2, ArrayPosition, Quaternion.identity).GetComponent<Rigidbody2D>();
            Asteroid2Array[i].gameObject.SetActive(false);
        }

        for (int i = 0; i < LargestAsteroidArraySize * 4; i++)
        {
            Asteroid3Array[i] = GameObject.Instantiate(Asteroid3, ArrayPosition, Quaternion.identity).GetComponent<Rigidbody2D>();
            Asteroid3Array[i].gameObject.SetActive(false);
        }

        for (int i = 0; i < LargestAsteroidArraySize * 8; i++)
        {
            Asteroid4Array[i] = GameObject.Instantiate(Asteroid4, ArrayPosition, Quaternion.identity).GetComponent<Rigidbody2D>();
            Asteroid4Array[i].gameObject.SetActive(false);
        }
    }

    public void SpawnAsteroid(int asteroidIndex)
    {
        if (asteroidIndex == 1)
        {
            Asteroid1Array[iteratorArray[0]].gameObject.SetActive(true);
            Asteroid1Array[iteratorArray[0]].position = Vector2.zero;
            if (iteratorArray[0] >= LargestAsteroidArraySize * 2) iteratorArray[0] = 0;
            else iteratorArray[0]++;
        }
    }
}

The working code (lower in the same script, thus with the same Start function):

    public void OnAsteroidDestroyal(GameObject CurrentAsteroidPrefab, Rigidbody2D CurrentRB)
    {
        if (CurrentAsteroidPrefab.name == "Asteroid_1(Clone)")
        {
            Asteroid2Array[iteratorArray[1]].gameObject.SetActive(true);
            Asteroid2Array[iteratorArray[1]].position = CurrentRB.GetComponent<Rigidbody2D>().position + new Vector2(-5f, -5f);
            Asteroid2Array[iteratorArray[1]].velocity = CurrentRB.GetComponent<Rigidbody2D>().velocity;
            if (iteratorArray[1] >= LargestAsteroidArraySize * 2) iteratorArray[1] = 0;
            else iteratorArray[1]++;

            Asteroid2Array[iteratorArray[1]].gameObject.SetActive(true);
            Asteroid2Array[iteratorArray[1]].position = CurrentRB.GetComponent<Rigidbody2D>().position + new Vector2(5f, 5f);
            Asteroid2Array[iteratorArray[1]].velocity = CurrentRB.GetComponent<Rigidbody2D>().velocity;
            if (iteratorArray[1] >= LargestAsteroidArraySize * 2) iteratorArray[1] = 0;
            else iteratorArray[1]++;
        }
//repeated four times for four arrays, not bothering you with that

I’ve tried making the arrays public but that doesn’t seem to help. What am I doing wrong?

What line is the error on?

1 Like

Asteroid1Array[iteratorArray[0]].gameObject.SetActive(true);

and it moves to

Asteroid1Array[iteratorArray[0]].position = Vector2.zero;

when I comment it

EDIT: it also moves to

if (iteratorArray[0] >= LargestAsteroidArraySize * 2) iteratorArray[0] = 0;
else iteratorArray[0]++;

so I guess the problem is with the int array

EDIT2: The error appears on the first two lines even when I hardcode a number in instead of referrencing the array, so I guess the problem has to do with both

Where do you populate your iteratorArray? I see you initialize it, but you don’t assign any values to it.

1 Like

Well shouldn’t all the values be defaulted to 0? The 2nd example, which works with the array as well, works as it is supposed to

You are correct. Moment of stupid on my part. The only other option is your Asteroid1Array has null values in it. When you make it public, is it fully populated?

1 Like

Yep, 5 elements, each is a rigidbody of a unique object in the scene.
Also, for some reason, the Asteroid1Array[iteratorArray[0]].position = Vector2.zero; line throws a OutOfRange exeption when I make the IteratorArray public. This doesn’t seem to happen with the second example.

So okay the problem seems to be me calling the function out of a different script for some reason. It works well when I call it out of the same script

I would show the other script. Sounds like you’re calling another instance of the script where the arrays are not initialized.

Or you’re trying to access the script in Awake/Start from another script before the values are assigned in this script, which would give you the error.

1 Like

Oh god right, I’m accessing the function out of a Start of another script, that has to be the problem. I can’t try it out right now, but it’s the most reasonable answer I can think of. Thanks a lot