- Home /
Destroying a GameObject w/ children isn't properly destroying everything?
I'm instantiating a GameObject (A Parent, with 12 Children) when a player makes a UI selection. When the UI is unselected it then Destroys the previously instantiated GameObject, so only one should remain at a time. (Similar to radio buttons on applications or the web, you can only have one as true at a time)
I have created a prefab which is a GameObject with the tag "display", which has 12 children with the tag "parts".
The scene has NO GameObjects in the scene from the start besides UI. They are only added/removed from the code below.
When I run my scene, and make my FIRST UI selection, it outputs that there are 12 GameObjects with tag "parts". The second selection, and every selection thereafter all output that there are 24 GameObjects with the tag "parts", never more, never less than 24. I can manually check through my Hierarchy, and only count 12, so why is it outputting there is 24?
Any help appreciated, thank you. Sorry if I didn't explain correctly, or it's a stupid question, I'm a bit new to c# and unity. I have my code below, with some commenting
public void uiSelection()
{
//search for GameObject with tag "display" (A parent GameObject w/ multiple Children)
//there should only ever be ONE, as it first checks for one
//before creating, and if exists, destroys the Parent (It should be destroying the Children too in the proccess, as it removes them from the Hierarchy)
GameObject a = GameObject.FindGameObjectWithTag("display");
if (a != null) Destroy(a);
//Instantiates a new GameObject from a prefab in my Assets/Resources
//itemDisplay is an Array of prefabs, loads according to location in the UI
//sets the local scale of the newely Instantiated object (Scale will vary, right now it's the same at 1.5f)
GameObject b = Instantiate(itemDisplay[uiLocation],new Vector3(5.50f,3.00f,0f),Quaternion.identity) as GameObject;
b.transform.localScale = new Vector3(1.5f,1.5f,1f);
//search for GameObjects with tag "parts"
//the prefab Parent has only 12 children with tag "parts"
//since it is destroying existing one before creating, there should never be more than one Parent w/ 15 Children GameObjects w/ tag "parts"
//the length of the list displayParts should never be greater than 12
displayParts = GameObject.FindGameObjectsWithTag("parts").ToList();
//For the very first time this code is ran, is outputs 12
//every time after the first, it will output 24, never more, never less
Debug.Log(displayParts.Count);
}
Please paste the code as it is in the script, its easier to see what you are trying to accomplish.
This might be related to the fact, that Destroy is not immediate. Do you still have 24 objects in next frame?
Bruno Tavares, I have edited my post, hope that helps.
ArkaneX, I do have 24 objects the next frame, I just checked for you
Thank you Nose$$anonymous$$ills. You are correct and was @ArkaneX. It was my error twice. When I replied to ArkaneX and said I checked if it had 24, I totally just had it output the list count of the list which was generated the line after it was destroyed. I didn't realize it until your comment. Sorry ;( I wish I could mark your comment as answer, but it does not allow me since I am so new. Thank you for your time both of you!
@chrisf112233: Nose$$anonymous$$ills' comment converted to answer, so you can accept :)
Answer by NoseKills · Jan 05, 2015 at 11:36 PM
Can you double check and duplicate just the counting bit and place it as the first thing to the top of the method and try again.
Debug.Log(GameObject.FindGameObjectsWithTag("parts").ToList().Count);
I'm not at all surprised if your code counts too many "parts" since like @ArkaneX said, Destroy is only guaranteed to have removed the objects in the next Update(). This would be my guess too.
Objects are destroyed after the current Update() (before rendering) but before the next Update() cicle. Just in case it makes a diference.
http://docs.unity3d.com/ScriptReference/Object.Destroy.html
"Actual object destruction is always delayed until after the current Update loop, but will always be done before rendering."
http://docs.unity3d.com/ScriptReference/Object.DestroyImmediate.html
Destroy is always delayed (but executed within the same frame).
Your answer
![](https://koobas.hobune.stream/wayback/20220613175633im_/https://answers.unity.com/themes/thub/images/avi.jpg)