• 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
0
Question by Digital-Phantom · Mar 22, 2015 at 04:53 PM · c#gameobjectparentfindchild object

How do I check if a child object exists?

At certain points in my game the PlayerController/object picks up another gameObject which temporarily becomes a child of the player. (I say temporarily as the picked up object has a destroy by time script on it)

Anyway... how can I check if this child object is present on the parent?

???

Comment
Add comment · Show 3
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 Owen-Reynolds · Mar 22, 2015 at 05:05 PM 1
Share

Is the temporary part really causing a problem? Seems like you could just forget that and search "unity find child."

It's not like the system cares if you just added the child last frame, or are going to remove it next frame. Whenever you check, the child is either there or it's not.

avatar image Digital-Phantom · Mar 22, 2015 at 05:14 PM 0
Share

Hmm, see what you mean...lol

Have actually checked through many answers, I've come across transform.Find, FindChild, transform.name, the list goes on..

Tried a few but keep co$$anonymous$$g up with compiler errors. It seems its one of those things that has no definitive answer.

:/

avatar image NoseKills · Mar 22, 2015 at 07:20 PM 1
Share

Compiler errors or run time errors? If you have compiler errors, you can't even press "play" to run the game and that means you have syntax errors, typos or are just simply using the functions wrong. That kind of errors should be easy to fix if you just show us your code.

In a script on your player controller, you can check for a certain child with

 transform.FindChild("nameOfPickedUpChildObjectToCheck");

like @Owen Reynolds said

5 Replies

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

Answer by sprawww · Mar 22, 2015 at 07:53 PM

 Transform[] ts = GetComponentsInChildren<Transform>();
         foreach(Transform t in ts)
         {
             if(//your condition here)
             {
                 //do something
             }
         }



Comment
Add comment · Show 6 · 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 sprawww · Mar 23, 2015 at 08:55 AM 0
Share

Thanks for accepting. Also, depending on how expensive your if statement is, you might want to add a break; after //do something, as in:

 if(//your condition here)
 {
 //do something
 break;
 }
avatar image isteffy · Mar 11, 2016 at 11:34 AM 0
Share

This solution will find if the child exists but not if the child does not exist

avatar image Lo0NuhtiK · Mar 11, 2016 at 11:45 AM 2
Share

isteffy · 5 $$anonymous$$utes ago 0 This solution will find if the child exists but not if the child does not exist

The topic is "How do I check if a child object exists?" ...and if you check if something exists, then you'll learn the answer to either question.

Look in the fridge for a sammich. If there's one in there then you know one exists. If there isn't one in there, then one doesn't exist.

If DoesExist = true, then DoesNotExist = false.
If DoesExist = false, then DoesNotExist = true.

avatar image isteffy Lo0NuhtiK · May 21, 2016 at 08:28 PM 0
Share

Ah yes you are correct. I was not seeing the 'break;' in your loop which would halt the loop after finding the sandwich. my b +1 for you

avatar image afakul · Jul 31, 2021 at 11:25 AM 0
Share

the sadnese is i dont know how to put what in your condition here

avatar image rage_co afakul · Jul 31, 2021 at 01:05 PM 0
Share

what do you want to check for? if it's for children...you can simply check

 if(ts.Length != 0)
 {
   //Do Something
 }

this means that the statement will execute when any child exists

avatar image
5

Answer by mjcollum · Aug 28, 2015 at 07:03 AM

old thread but for anyone looking

transform.childcount returns an int. if this is 0 it has no children.

http://docs.unity3d.com/ScriptReference/Transform-childCount.html

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
0

Answer by Mickwa · Mar 22, 2015 at 07:48 PM

Maybe just comparing transform to null will solve Your problem? Example:

 using UnityEngine;
 using System.Collections;
 
 public class Test : MonoBehaviour
 {
 
     GameObject go;
     Transform t;
     void Start()
     {
         go = new GameObject();
         t = go.transform;
         Destroy(go, 2f);
     }
 
     void Update()
     {
         if (t == null) 
             Debug.Log("Object does not exist");
         else
             Debug.Log("Object exists");
     }
 }


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
0

Answer by Riley Smit · Apr 21, 2016 at 11:38 AM

You can use transform.childCount

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
0

Answer by JeffBrin · Jan 14 at 03:38 PM

Old thread but if anyone needs, you could use a try catch

 bool childExists = false;
 try
 {
     child = GetComponentInChildren<Transform>(); // Replace Transform with any other script that would only be on this child
     childExists = true;
 }
 catch{
 }

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

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

32 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Get nearest GameObject 1 Answer

(Solved) Findout if an object exist in the scene, but dont trow exception 1 Answer

Center of a GameObject with Bounds? 1 Answer

Activate all child objects in a Parent object 2 Answers

Can not access GetComponentInParent ( ). usegravity 1 Answer


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