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)
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.
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:
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.