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?


old thread but for anyone looking

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

Transform ts = GetComponentsInChildren();
foreach(Transform t in ts)
if(//your condition here)
//do something

You can use transform.childCount

Maybe just comparing transform to null will solve Your problem?

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");
            Debug.Log("Object exists");

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

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