1

I am not sure why but this is throwing

NullReferenceException: Object reference not set to an instance of an object
GridSquareController.Update () (at Assets/Scripts/GridSquareController.cs:53)

Code:

for (int i = 0; i < numOfPrefabs; i++)
{
    System.Random rnd = new System.Random();
    int squareValue = rnd.Next(1, 6);
    gridBehaviour = spawnedPrefabs[i].GetComponent<GridBehaviour>();
    gridBehaviour.gridValue = squareValue;
    Debug.Log(gridBehaviour.gridValue);
}

I am trying to set a value on the scripts of a series of prefabs I am also initializing in another part of this code.

But every time I press the key that activates this for loop it throws the same error.

Section where prefabs are initialized:

void SpawnPrefabs()
{
    //will create as many prefabs as I specify in the numOfPrefabs variable
    if (numOfPrefabs > 0)
    {
        for (int i = 0; i < numOfPrefabs; i++)
        {
            GameObject newSpawnedPrefab;
            //instantiates a new prefab
            newSpawnedPrefab = Instantiate(gridTilePrefab, new Vector3(i, 0, 0), Quaternion.identity);
            //parents the new prefab to the transform of the current object (just parenting it really)
            newSpawnedPrefab.transform.SetParent(transform);
            //adding the prefab to the array
            spawnedPrefabs.SetValue(newSpawnedPrefab, i);
        }
    }
}

Also here's the bit where I defined my variables

// the prefab I am instantiating into the gameworld
public GameObject gridTilePrefab;
public GridBehaviour gridBehaviour;

// an array containing all of the spawned prefabs
public GameObject[] spawnedPrefabs;

// the number of prefabs I want spawned calculated from the length of the array containing the prefabs
public int numOfPrefabs;
public bool Jeremy;
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • There is a lot that could be wrong here but you haven't shown enough of your code to actually figure out what you forgot to initialize... Try stepping through your code in the debugger and figure out what you have forgotten to initialize. – WBuck Dec 17 '21 at 00:46
  • Dont put new System.Random() inside a loop. Also unity has a built in random that may work better for you. – Scott Chamberlain Dec 17 '21 at 04:36
  • That tip about Unity's random class sounds useful I will look into that. – Byron Speir Dec 19 '21 at 22:09

2 Answers2

1

spawnedPrefabs are not initiated. Add spawnedPrefabs= new GameObject[numOfPrefabs] to your code

void SpawnPrefabs()
    {
        //will create as many prefabs as I specify in the numOfPrefabs variable
        if (numOfPrefabs > 0)
        {

          spawnedPrefabs= new  GameObject[numOfPrefabs] ;

            for (int i = 0; i < numOfPrefabs; i++)
            {
                GameObject newSpawnedPrefab;
                //instantiates a new prefab
                newSpawnedPrefab = Instantiate(gridTilePrefab, new Vector3(i, 0, 0), Quaternion.identity);
                //parents the new prefab to the transform of the current object (just parenting it really)
                newSpawnedPrefab.transform.SetParent(transform);
                //adding the prefab to the array
                spawnedPrefabs.SetValue(newSpawnedPrefab, i);

            }

        }

    }
Serge
  • 40,935
  • 4
  • 18
  • 45
  • Most probably not. Since it is public and `GameObject` a serialized type the Unity Inspector/Serializer would usually initialize that array automatically – derHugo Dec 17 '21 at 08:25
0

It could be because of this line. I'm not sure.

gridBehaviour = spawnedPrefabs[i].GetComponent<GridBehaviour>();
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 17 '21 at 02:47
  • I am looking at that line to try to find an issue. Thank you for the tip off. I might try a foreach loop instead. – Byron Speir Dec 19 '21 at 22:10