- Home /
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());
Answer by Legend_Bacon · Jun 28, 2018 at 03:58 PM
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 ins$$anonymous$$d. Destroying the transform component is not allowed.
Thanks
Ah, of course. Sorry about that.
Indeed, you cannot destroy a transform directly. But you CAN destroy the gameObject it's attached to.
Just replace the Destroy() call with : Destroy(instantiatedObject.transform.GetChild(i).gameObject);.
I've updated my code above accordingly.
Hope that helps!
~Cheers,
LegendBacon
Answer by tormentoarmagedoom · Jun 28, 2018 at 01:32 PM
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!
When it comes to destroy it, whether I try Destroy or DestroyImmediate, it doesn't work.. :/ I used used GameObject.Find("stringName") for the test. It's on runtime and I would like the program to load a 3ds from a previous program (sketchup) and just kill the camera. Here are the error message.
-If you really want to remove an asset use DestroyImmediate (theObject, true); -If you want to destroy the game object, please call 'Destroy' on the game object ins$$anonymous$$d. Destroying the transform component is not allowed. Here is the code
obj = Resources.Load("3") as GameObject;
Instantiate(obj); DestroyImmediate(obj.transform.Find("skp_camera_Last_Saved_SketchUp_View",true);
obj.Find don't work. thank you so much for helping me
Your answer
Follow this Question
Related Questions
Instantiated objects not appearing in scene or game view 2 Answers
Instantiating an object to the location of child of another object. 2 Answers
Get child component of a prefab to reference with another prefab 1 Answer
how to make a Portal 2-esque laser beam "redirection" system? 0 Answers
Instantiate at Child Object 2 Answers