- Home /
Loop through a GameObject list based on the size of the list
Hey i have a list of GameObjects that i want to spawn in the scene with Instantiate. Here is the trick tho: I want to loop through the GameObject list and spawn each element based on the size of the list, then it has done its job and should stop looping. It will go through each index, from lowest to highest in the list.
Is there a way to end a foreach loop once a certain criteria has been passed? While i need to count on a int everytime i instantiate a object and have a while loop that loops until that int has reached a certain number?
Thanks
Not really sure what the behavior you want is. Please provide more detail in your question.
Post your code and explain the intended behavior step by step.
Answer by JedBeryll · May 28, 2016 at 08:12 PM
Not sure what you are saying. You have a while loop? If so you already have a check.
int i = 0;
while (i < objects.count) {
//do stuff
}
----------
for (int i = 0; i < objects.count; i++) {
//do stuff
}
----------
foreach (GameObject obj in objects) {
//do stuff
}
do {
//do stuff
} while (i < objects.count);
if you need extra conditions you can use "break" to break out of an iteration or "continue" to skip the current cycle.
Your answer
Follow this Question
Related Questions
How to skip certain objects in List, in a for loop? GUI related. 1 Answer
A node in a childnode? 1 Answer
How do I Instantiate a prefab in a list? 0 Answers
Looping through multiple lists 0 Answers
problem with loop and mesh values 0 Answers