- Home /
Adding GameObject to GameObject[] array
Hello everyone, thank you for your time.
Quick question, I've been looking at code for almost 8 hours in a row and I just can't tell on such small errors. Correct me if Im wrong but. Im trying to do something similar.
Im trying to get every element inside an array(gameObject) that matches certain criteria. If it does, it goes inside the newElement. Like this!
public GameObject[] newArray;
public GameObject[] oldArray; //This array is set on the inspector. (Dragged the GO's inside)
Start(){
int tempCounter;
for(cnt=0;cnt<= oldArray.Length; cnt++){
if(matches Criteria){
newArray[tempCounter] = oldArray[cnt];
tempCounter++;
}
}
}
But I keep getting a IndexOutOfRange error.
I don't think i have to hardcode the size of the array, do I?(It's because the size of the array depends on wether it matches the criteria or not.... Yes. I've checked that both, the array and the item are gameobjects....
Help? Please...The coffee is just not doing its job anymore. Thank you for your help and time!
Answer by Spinnernicholas · Oct 07, 2014 at 01:05 AM
You have to initialize the array first, and yes you have to initialize it with a size. C# Arrays
If you want something with a dynamic size, use a List. or Here.
Your answer
Follow this Question
Related Questions
Creating an array of prefabs? 4 Answers
Enabled all gameobjects in array 1 Answer
Checking if a position is occupied in 2D? 1 Answer
Array out of index? 1 Answer