Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
-1
Question by Rodolfo-Rubens · Sep 29, 2015 at 09:04 AM · gameobjectscenehierarchycheckexists

How to check if GameObject exists in hierarchy (is instantiated)

How do I check this? Is there a way to do this? It may seems like a dumb question (it may be) but when working with prefabs nothing works, activeInHierarchy, the GameObject bool operator, nothing works because in fact the object exists because of the prefab, checking those will return true but they are not strict to the scene hierarchy, it can be "active in hierarchy" but not on the scene...

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
-1
Best Answer

Answer by Rodolfo-Rubens · May 01, 2017 at 05:03 PM

It seems like I need to null the reference to the object that I'm checking if it exists, even when the object gets destroyed because you can still access the object after it gets destroyed and if I null check it it will return false (in the inspector it will show as Missing, the reference is not null which is pseudo not destroyed in this case, but I destroyed it so I want it to be null), so I needed to make sure the reference is manually nulled after the object got destroyed.

BTW, this weirdness only happens in the same frame that the object gets destroyed, need to wait a frame before null comparing the object, if you wait you will not get not null for the object destroyed. This is needed for OnDestroy do its thing.

Checking if objects are active and spawned in the Hierarchy (if you have a field that can contains prefabs and spawned objects) I don't know if this is possible.

I just stumbled onto this again: https://forum.unity3d.com/threads/object-null-returning-true-but-object-doesnt-exist-anymore.468762/

Comment
Add comment · 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
-1

Answer by Denvery · Sep 29, 2015 at 10:39 AM

As a way, you can assing specific tag to you prefab, then call GameObject.FindWithTag("YourTag"). This function will return one active GameObject with this tag, if exist

Comment
Add comment · Show 2 · 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 Rodolfo-Rubens · Sep 29, 2015 at 01:54 PM 1
Share

This is not viable for me because there may be a lot of GOs with the same tag n the scene... Thanks for replying.

avatar image Denvery Rodolfo-Rubens · Sep 29, 2015 at 01:59 PM 0
Share

You can call FindObjectsWithTag. It will find all active objects with this tag

avatar image
-1

Answer by Sickwitit · Sep 29, 2015 at 02:23 PM

Like this? You could also change the string parameter to GameObject and change the if condition accordingly.

 public bool IsChildAliveInParent(Transform parent, string childToCheck)
 {
     bool isChildAlive = false;
     for(int i = 0; i < parent.childCount; i++)
     {
         if(parent.GetChild(i).Equals(childToCheck))
         {
             isChildAlive = true;
             break;
         }
     }
     return isChildAlive;
 }
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 Rodolfo-Rubens · May 01, 2017 at 05:09 PM 0
Share

This is a good solution, although, my object is in the root so my parent will be null...

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

Game Object disappear after move to another Scene 0 Answers

Object not visible in Scene tab until double click it on the Hierarchy. 1 Answer

win game & loos game 0 Answers

Help me! Create a game object falling from the top scene at click mouse point! T________T 0 Answers

Unity removes all objects includes the main camera in Hierarchy when i start test play 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