For loop not working
I'm making a game in C#, and the key concept of the game is instantiating platforms vertically. In one of my scripts for managing the instantiation of platforms, I have a for statement that will instantiate 4 initial platforms and the recycling of platforms is done in another script.
public Transform platPrefabBottom;
public Transform platPrefabTop;
List<Transform> prefabList = new List<Transform>();
int prefabIndex = 1;
public static int platInstNum = 0;
// Use this for initialization
void Start () {
prefabList.Add(platPrefabBottom);
prefabList.Add(platPrefabTop);
for(int i = 0; i < 4; i++)
{
Instantiate(prefabList[prefabIndex], new Vector3(0, platInstNum, 0), Quaternion.identity);
platInstNum = platInstNum + 6;
if(prefabIndex == 1)
{
prefabIndex++;
}
else if(prefabIndex == 2)
{
prefabIndex = 1;
}
print ("done: " + prefabIndex);
}
}
As you can see, I have a for statement that is supposed to loop 4 times, but instead only runs through once. I have told the code to print "done" for confirmation, and it only prints once.
Not sure why this is happening, could anyone give me some help? Thanks
I noticed a while back that sometimes prints and debug.logs dont work effectively in a for loop. The two functions are particularly slow and often cant keep up with the execution speed of a for loop. Dont rely on this.
Are you not simply instantiating the same objects on top of each other?
No, I've already checked if I'm instantiating them on top of each other and no they aren't but thanks for the reply.
It should spawn 2 of each right? 2 vertically placed objects alternating twice, spaced by 6?
Answer by Chris-Paul · Oct 09, 2015 at 10:57 AM
Ok so your prefab list has only two elements, the index of the first is 0, and the second element's index is 1, the problem is that you let your "prefabIndex" to reach the value of 2, which it's outside of your list's boundaries, therefore you probably get an error when you try to acces prefabList[2].
This is the correct code:
public Transform platPrefabBottom;
public Transform platPrefabTop;
List<Transform> prefabList = new List<Transform>();
int prefabIndex = 1;
public static int platInstNum = 0;
// Use this for initialization
void Start () {
prefabList.Add(platPrefabBottom);
prefabList.Add(platPrefabTop);
for(int i = 0; i < 4; i++)
{
Instantiate(prefabList[prefabIndex], new Vector3(0, platInstNum, 0), Quaternion.identity);
platInstNum = platInstNum + 6;
// This is correct.
prefabIndex ++;
if(prefabIndex >= prefabList.Count) {
prefabIndex = 0;
}
print ("done: " + prefabIndex);
}
}
Oh yeah, I always mess that up by starting arrays at 1 ins$$anonymous$$d of 0! I'll try this code first thing in the morning, thanks. EDIT: Never$$anonymous$$d I tried it now, and it works! Thanks!
Your answer
Follow this Question
Related Questions
How does one continuously move an object forward without using velocity? 1 Answer
Adding different values to randomly created objects 0 Answers
While instantiating getting object reference not set for no reason? 1 Answer
Prefab destroyed upon instantiation 0 Answers
Using One Script for Two Separate Parent GameObjects Failing 0 Answers