i am having trouble with lists and array’s and dictionaries.
i have a this piece of code
for(m = 0; m < amountOfPrefabs ; m++) // loop trew the objects
{
if (arrayOfPrefabObjects[m].activeInHierarchy == false)
{
key = listOfGameObjectNames[m];
print ("key = " + key);
// i use this to acces a dictionarie but i removed that part
}
}
this results in ; “cubes1,cubes2,cubes3,cubes4,cubes5” as expected since they are in my list.
Sure. To solve problems like this, you have to sort of “be the computer” and think about what your code would do line by line (rather than what you intended it to do). In this case, the while loop starts at 0. It removes item 0 (cubes1). It’s a list, so all the others shift down; now cubes2 is item 0, cubes3 is item 1, etc. Then you increment n, so now n=1, and we remove item 1 (cubes3), and the others shift down so cubes2 is item 0, cubes4 is item 1, cubes5 is item 2, etc. On the next pass, n=2, so we remove item 2 (cubes5), and so on.
Basically, the code is doing exactly what you told it to.
If you want to remove all the items, just use the Clear method. If you want to iterate over them while removing them (because you’re also going to do some other stuff, I presume), then your current loop will work fine if you simply remove the “n++” line so that you’re always examining and removing the first item in the list. (Or you could work backwards through the list, removing things from the end.)
Thanks for your reply.
It seems you are a better “computer” then i am. I tried to think like you described. but i turned out to be more human.
Your idea seems to be the solution, ill try it as soon as i can and let you know.
Well, he can go backwards, or he can just keep removing element 0. Given his current code, removing element 0 is probably easier for him.
And III, don’t worry about it too much — it takes practice to think about what you’re actually saying, rather than what you mean by what you’re saying. Unfortunately, computers are very literal!
Thanks again for your replies.
I got it almost running. But now i am getting new problems. However i still need to figure out what causes that problem. (it turns out my test scene was to small scale for what i want to do with it)
But your insights have helped me getting a step further.