- Home /
While Loop Issue
Sooooo Unity continues its streak of lets confound this noob once a day. Simple Simple test code, heres its purpose: 1) Game tiles are created in a scrollable list. 2) If another menu category is selected I need to clean said list. I do so by destroying the created tiles 3) Easiest way to do this is a while loop knowing the sibling start position and the number that should exist at the end Heres code 1 from my test button (which means I hand jammed some values, its just easier then executing the big code every time)
public void OnPointerClick(PointerEventData data)
{
int x = 17;
while (x > 10)
{
Destroy(GameObject.Find("Menu").transform.GetChild(1).gameObject);
x--;
Debug.Log(x);
}
}
}
This code executed correctly once, I smiled (big mistake), and it hasn't executed correctly since. (restarted program and all) (Worth noting the time it worked I made x subtract with x=x-1;)
Also this would have been the shortest way to write this loop, why does this freeze the program?
while (GameObject.Find("Menu").transform.childCount > 10)
{
Destroy(GameObject.Find("Menu").transform.GetChild(1).gameObject);
}
Wouldn't the act of deleting a child change the childcount? On an off note I think I found the cure for low blood pressure.
DestroyImmediate was the answer, not sure why it worked the one time.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Unity "unequip" selected item from inventory. 1 Answer
Destroying Objects. 1 Answer