Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by Saga17 · Jun 28, 2018 at 01:27 PM · instantiatechild objectdestroyimmediate

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());

Comment
Add comment
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

2 Replies

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

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

Comment
Add comment · Show 2 · 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 Saga17 · Jun 28, 2018 at 05:37 PM 0
Share

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

avatar image Legend_Bacon Saga17 · Jun 28, 2018 at 09:46 PM 1
Share

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

avatar image
0

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!

Comment
Add comment · Show 1 · 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 Saga17 · Jun 28, 2018 at 03:35 PM 0
Share

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

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

108 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 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 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 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

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


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