Problems with loops
Ok, this is likely to be a pretty difficult one for you to help me with, but, anyway... I'm making a game. At one point, you fly into a portal, stuff happens. One of them things is it should destroy all objects with tag 'obstacle'. This works perfectly the first time. The second time, however, it doesn't. Here's the code:
rocks = GameObject.FindGameObjectsWithTag("obstacle");
for (var i = 0; i < rocks.Length; i++)
{
Destroy(rocks[i]);
}
I know the function that that's contained in is run both times, but... like I said, the obstacles stay the second time round.
Weirdly, two other loops are run when the player flies into the portal in another script and neither of them work the second time either. Here's one:
foreach (Transform child in transform)
{
GameObject.Destroy(child.gameObject);
}
and here's the other:
foreach (GameObject i in Planets)
{
i.transform.parent = transform;
}
So, I know the problem seems pretty vague, but I'd appreciate any ideas. I'm completely clueless as to why they'd only work once.
Thanks.
EDIT: Just to include what I said below, changing that second code snippet to
foreach (GameObject i in Planets) { Destroy(i); }
fixes it.
Still stuck with the other two though...
Answer by NornIndustries · May 20, 2016 at 09:06 PM
Depending on how quick you fire this event the second time, it may be that Unity is being slow on garbage collection. I noticed recently that calling Destroy() doesn't guarantee that the object will be destroyed right then.
So what you can do is to first make it that when a rock is created it is also added to a static list of rocks. Then when you do your foreach, you both destroy the objects on that list AND remove them from the list (important!). Then when it checks the list the second time they at least won't be on the list, even if they are still sort of existing somewhere waiting to be destroyed. I hope that helps!
The event is fired anywhere from about 10 seconds to a few $$anonymous$$utes later, so not so quickly. Putting all rocks in a list may be a bit difficult. There are groups of rocks as children of separate objects, so while adding them to a list in the script attached to their parent is easy, collating that list and then removing the groups from the list on time in the correct order could be quite problematic.
EDIT: Interestingly, changing the second snippet of code to foreach (GameObject i in Planets) { Destroy(i); }
made that work.
Answer by Jake_Martin_ · May 20, 2016 at 09:48 PM
I fixed this. The problem was to do with not removing planets from their list properly after destruction. Changing that made everything else work :)
Your answer
Follow this Question
Related Questions
How to reference assets that are inside a folder?,Referencing assets in the scene 0 Answers
Camera Issue,Camera is changing it's position in game view. 0 Answers
Input Command Issues 0 Answers
My gameobject stops moving after entering the trigger?? 2 Answers
How to project a semi transparent copy of a gameobject? 0 Answers