Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by chrisf112233 · Jan 05, 2015 at 11:54 AM · instantiatelistdestroy

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);
 }
Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Codessaurus · Jan 05, 2015 at 01:22 PM 0
Share

Please paste the code as it is in the script, its easier to see what you are trying to accomplish.

avatar image ArkaneX · Jan 05, 2015 at 01:39 PM 1
Share

This might be related to the fact, that Destroy is not immediate. Do you still have 24 objects in next frame?

avatar image chrisf112233 · Jan 05, 2015 at 11:16 PM 0
Share

Bruno Tavares, I have edited my post, hope that helps.

ArkaneX, I do have 24 objects the next frame, I just checked for you

avatar image chrisf112233 · Jan 05, 2015 at 11:50 PM 0
Share

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!

avatar image ArkaneX · Jan 06, 2015 at 12:15 AM 1
Share

@chrisf112233: Nose$$anonymous$$ills' comment converted to answer, so you can accept :)

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Codessaurus · Jan 08, 2015 at 02:14 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

C# override a destroyed GameObject in a List 1 Answer

Instantiate prefab, add to list, destroy older prefab 1 Answer

Store Game Object Into List For Later Reinstantiation? 0 Answers

Destroy and rebuild game objects from lists 1 Answer

Instantiate prefab and Add to a Generic List 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges