find/destroy camera in instantiated object

Hey,

I try to find a camera in an instantiated object but after numerous attempts, I just can’t make it work… I would like to destroy the camera with DestroyImmediate but first I have to find it. Is it possible to find a child that contain speficic character in its name?

Here are the code lines. It return ‘null’. How can I find this camera?? Thank you!

obj = Resources.Load(“2”) as GameObject;

Instantiate(obj);

Debug.Log("Equipped item at remove: " + obj.transform.GetComponent());

You have 3 options.

1, save the gameobject into a variable when you instantiate it.

2, use GameObject.Find(“stringName”);

3, assign a tag to the object and use GameObject.FindObjectWithTag(“the tag”);

And use Destroy(GameObject), it will be destroyed at the end of the frame.

Bye!

Hello there,

There are multiple ways to find an object. In your case, I recommend you try this:

        obj = Resources.Load<GameObject>("3");
        GameObject instantiatedObject = Instantiate(obj);

        //Here find the camera by name and destroy it
        for (int i = 0; i < instantiatedObject.transform.childCount; ++i)
        {
            if (instantiatedObject.transform.GetChild(i).gameObject.name.Equals("The_name_of_the_object_you_want_to_destroy"))
                Destroy(instantiatedObject.transform.GetChild(i).gameObject); // You could also use DestroyImmediate if you prefer...
        }

I hope that helps!

~Cheers,

LegendBacon

I can’t destroy the transform, by one way or the other. Unity tell me this: If you want to destroy the game object, please call ‘Destroy’ on the game object instead. Destroying the transform component is not allowed.

Thanks