- Home /
Instantiate object in for loop with array
So i am instantiating a GameObject which works just fine. Now i want to be able to save different instances of that GameObject and save those in an array and then display those via a for loop. For some reason My for loop isnt even executed.
here is the Code:
for(var i : int = 0; i <= eventsArray.length-1; i++){
print("i: "+i);
DayDisplayInstance = Instantiate(DayDisplay, new Vector3(56.39999, -222.8), transform.rotation);
DayDisplayInstance.transform.SetParent(startScreenPnl.transform, false);
eventsArray.Add(DayDisplayInstance);
DayDisplayInstance = eventsArray[i];
child = DayDisplayInstance.transform.GetChild(0).gameObject;
myText = child.GetComponent.<Text>();
myText.text = "MO"; //Add variable instead of MO to make the event flexible
}
If i Instantiate and add to the array outside of the loop it workds but that ofc will not allow me to instanciate multiple times in that one function, that's why i want to do it in the for loop.
My guess would be that the array is size 0 in the beginning and nothing is being added untill after the for loop. So the for loop says it starts at 0 and ends at array.length which since the array is empty is also 0. This results in the code in the for loop not being executed. How would i fix this? Any ideas? Anything would be much appreciated!
Thanks in advance!
.
(the Vector3 is only for testing for now. My thought would be to make an array with vectors and then looping through that with i as well to display those GameObjects in the right position.
Hi,
I don't know UnityScript (that's why I write a comment and not an answer), but I think you expand your eventsArray
within the loop when you use eventsArray.Add(DayDisplayInstance);
. In C# the Add()
function of List<T>
expands the size of as necessary and assigns the value. So your loop will never finish, because eventsArray.length
keeps increasing
Ins$$anonymous$$d you should first initialize your eventsArray
to the desired size (which I think you already do), and then just use eventsArray[i] = DayDisplayInstance;
.
Also, the line DayDisplayInstance = eventsArray[i];
is unnecessary, because you just assigned DayDisplayInstance
to eventsArray[i]
.
Answer by Nighfox · Jan 30, 2018 at 02:26 PM
EDIT: Actually, it will not just not execute your code, but it will also crash Unity and cause massive memory consumption over time. @Harinezumi had already mentioned about it.
@Romano's answer works fine, but it needs a little bit of fix as that in itself may cause bugs later on. Here's what I've noticed so far:
1. Is eventsArray
even an array? You're using its length
property (which exists on arrays and not Lists) but then you're also using Add
, which only is possible if it's a generic List
.
2. You're adding items to eventsArray
while accessing its actual length, which means this will continue to loop forever and ever.
Workaround: Just create a temporary variable to store the previous length of the list, then use that in your for
loop.
var length = eventsArray.Count;
for(var i : int = 0; i < length; i++){
//...your code here
}
Somewhere in your code, (since you want to use Add
), eventsArray
should be declared as a List
:
import System.Collections.Generic;
public var eventsArray : List.<GameObject>;
And initialize it like this:
eventsArray = new List.<GameObject>();
as i said no it doesnt execute the code below since the first eventsArray.Add(..) is within the loop which it kind of needs to be there to do what i want. So what kind of a work arround could i do? Im lost but i need this to work. It's the last thing that i need to get to work pretty much. Thanks in advance again!
Use the variable / constant of the number of objects you want to instantiate in the loop condition (but use < ins$$anonymous$$d of @Nighfox this is why I deleted my comment, in the end Romano's answer will be correct :D :D :D
well the number of GameObjects is exactly array.length-1 It's dynamic. Every time the fucntion gets called its one more.. Wait. I have an idea. I could make a counter that increases every time the function is called ( i have a function that is called by a button). That might work
Answer by Romano · Jan 30, 2018 at 02:05 PM
I think if you change this line
for(var i : int = 0; i <= eventsArray.length-1; i++){
to read
for(var i : int = 0; i <= 5; i++){
where 5 is the number of game objects you want to instantiate it should run. You would just replace the number 5 with whatever variable you want if the loop number needs to be dynamic.
In that case it won't be that dynamic as it would have been if it took the length property of array.
If the events array is empty in the first place, I think that still won't run.
@Nightfox you wrote almost the same comment as the one I deleted :) But it doesn't solve the issue, because it is a logical bug ( eventsArray.Add(...)
is called within the loop, increasing the length...).
Nice catch, I didn't notice that lol. $$anonymous$$ight post a workaround for that.