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?
???
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.
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.
:/
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
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
}
}
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;
}
This solution will find if the child exists but not if the child does not exist
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.
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
the sadnese is i dont know how to put what in your condition here
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
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
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");
}
}
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{
}