NullReferenceExeption

Hello I got this code

using UnityEngine;
using System.Collections;

public class SpawnControls : MonoBehaviour {

    public string[] Word = new string[] {"Black","Blue","Brown","Green","Orange","Pink","Purple","Red","White","Yellow"};
    public string[] Color = new string[] {"Black","Blue","Brown","Green","Orange","Pink","Purple","Red","White","Yellow"};
    public string TempWord;

    public string ChoosenWord;
    public string ChoosenColor;

    public int ChoosenWordInt;
    public int ChoosenColorInt;

    public Transform[] Black;
    public Transform[] Blue;
    public Transform[] Brown;
    public Transform[] Green;
    public Transform[] Orange;
    public Transform[] Pink;
    public Transform[] Purple;
    public Transform[] Red;
    public Transform[] White;
    public Transform[] Yellow;

    private Transform[] FirstBox = null;

    public Transform[] spawnPoints;

    // Use this for initialization
    void Start () {

        ChoosenWordInt = Random.Range (0, 9);
        TempWord = Word [ChoosenWordInt];
        while ((ChoosenColorInt = Random.Range (0, 9)) == ChoosenWordInt);
        Debug.Log ("The Word is: " + Word [ChoosenWordInt] + " - The Color is: " + Color [ChoosenColorInt]);

        switch (ChoosenColorInt) {
        case 0: MakeFirstBox(Black);
            break;

        case 1: MakeFirstBox(Blue);
            break;
           
        case 2: MakeFirstBox(Brown);
            break;
           
        case 3: MakeFirstBox(Green);
            break;
           
        case 4: MakeFirstBox(Orange);
            break;
           
        case 5: MakeFirstBox(Pink);
            break;
           
        case 6: MakeFirstBox(Purple);
            break;
           
        case 7: MakeFirstBox(Red);
            break;
           
        case 8: MakeFirstBox(White);
            break;
           
        case 9: MakeFirstBox(Yellow);
            break;
           
            }


        Instantiate (FirstBox[ChoosenWordInt], spawnPoints [Random.Range (0, 3)].position, Quaternion.identity);
    }
   
    // Update is called once per frame
    void Update () {


   
    }

    // Applying the choosen Color to the FirstBox Array
    void MakeFirstBox (Transform[] FirstArray)
    {
        for (int i = 0; i<10; i++) {
            FirstBox[i] = FirstArray[i];
        }
    }

}

And im getting this single error, as i run the game

NullReferenceException: Object reference not set to an instance of an object
(wrapper stelemref) object:stelemref (object,intptr,object)
SpawnControls.MakeFirstBox (UnityEngine.Transform[] FirstArray) (at Assets/SpawnControls.cs:87)
SpawnControls.Start () (at Assets/SpawnControls.cs:46)

You should probably just have your loops match the actual array length rather than specifically telling it “10”, like:

for(int i = 0; i < array.Length; i++){

When i use Range(0,9) im pretty sure i will get these numbers:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9 = 10 numbers

I removed that because it was slightly misleading, but the fact is that you actually won’t- you’ll only get up to 8. This is because Random.Range() for ints was specifically designed for arrays in mind (so you can just toss in the array count as the max number), while Random.Range() for floats actually returns values in the manner that you’d expect.

I just debugged the range, and you are right, it only showed from 0 to 8, it is now changed to 0,10

But that didn’t solve the problem im still getting the same error

I try to debug the MakeFirstBox Funktion

// Applying the choosen Color to the FirstBox Array
    void MakeFirstBox (Transform[] FirstArray)
    {
        for (int i = 0; i<FirstArray.Length; i++) {
            FirstBox[i] = FirstArray[i];

            Debug.Log("FirstArray: "+FirstArray[i]);
            Debug.Log("FirstBox: "+FirstBox[i]);


        }
    }

And it is showing me that i doesn’t reach to the function, it is stuck in the Switch. why?

The placement of those debugs actually tell you nothing, except that the failure is happening on the very first loop. If you wanted to check if it’s getting to the function, then you should make a debug that’s the first line in the function. In this case, I’m guessing that it’s because FirstBox hasn’t actually been initialized. If you want to copy the entire array of FirstArray over, you can do so with a simple:

FirstBox = FirstArray; //assigns array reference itself
FirstBox = FirstArray.ToArray(); //copies array to new array

Otherwise, you need to initialize the array to the size of FirstArray and then copy the items over like so:

FirstBox = new Transform[FirstArray.Length];

for(int i = 0; i < FirstArray.Length; i++ )
{
   FirstBox[i] = FirstArray[i];
}
void MakeFirstBox (Transform[] FirstArray)
    {
        FirstBox = new Transform[FirstArray.Length];

        for (int i = 0; i<FirstArray.Length; i++) {
            FirstBox[i] = FirstArray[i];

        }
    }

That solved the problem!

Thanks

Could you please tell me what was going on? btw the Firstarray.Toarray() doesn’t work

It’s probably an extension method in System.Linq or System.Collections.Generic, as most good collection methods are.

But yeah, arrays can’t be dynamically resized, and they have to be initialized to a set size. In this case, the field was public on a monobehaviour script, so the Inspector was initializing it to some small size outside of this script, otherwise you would’ve gotten an “Array not Initialized” message- if you set it to private you’ll see what I mean.

So yeah, it was technically initialized, but had no slots, and since you can’t change an array’s size after it’s been created, you have to create a whole new array with the right size and replace it.

[EDIT: In response to a deleted question- so people don’t look at me funny…]

I guess you could make a helper function that you feed integers and it feeds you back an int list. Something like the following:

public List<int> RandomRangeList(int start, int range)
{
   List<int> randomList = new List();
   List<int> remaining = new List();
   for(int i = 0; i < range; i++)
   {
      remaining.Add(start + i);
   }
   while(remaining.Count > 0)
   {
      int randomIndex = Random.Range(0, remaining.Count);
      randomList.Add(remaining[randomIndex]);
      remaining.RemoveAt(randomIndex);
   }
   return randomList;
}

There’s probably a better/more efficient way to do this, but I can’t think of one at the moment.