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 samulihe · Aug 23, 2017 at 08:57 AM · gameobjectchildaccessing

Mysterious unaccessability to child gameobject

I try to be as simple and clear as I can: I have a 3D model as a child of an empty game object. This model has been instantiated from the empty gameobject's script. I tried to change material of this child, but nothing seemed to work. I tried to disable the renderer, nothing again. Last I started destroying these to see if I really access the right stuff. Ok, I destroy the parent, which worked and child was gone too. Then I tried to access and destroy the child and nothing happens. No errors, just nothing. I have no idea why this happens. Propably why I cannot do nothing with the materials either.

Empty game object has a private GameObject-field named prefabModel. It is instatiated this way:

 private GameObject prefabModel;
 . . .
 Object obj = Resources.Load("path/to/model", typeof(GameObject));
 prefabModel = Instantiate(obj, transform, false) as GameObject;

The way I try to access this child (p is the empty with the script mentioned just now):

 p.transform.GetChild(0);

I can print stuff this way no problem and destroy the parent with the child too:

 print(p.transform.GetChild(0).gameObject.name); // prints the name of child
 Destroy(p.gameobject); // parent destroys fine and child gone too ofc

BUT NOW comes the interesting part:

 Destroy(p.transform.GetChild(0).gameObject); // NOTHING HAPPENS!!!

I get no errors from this, nothing happens. So after this finding I made a method to the empty parent's script (where the child is instantiated) to return the field:

 public GameObject GetPrefabModel() {
         return prefabModel;
     }

And now if i try to destroy the child:

 Destroy(p.GetPrefabModel()); // Child is destoryed normally

I have not yet tried to change the materials of the object now that I have the GetPrefabModel()-function. But I will now after I have written this.

Can someone explain what exactly is going on with this? Maybe a bug with the GetChild()-function?

Or something funny the way I instantiate the child? (yes the child is in the hiearachy and gets destroyed along the parent too )

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by SteenPetersen · Aug 23, 2017 at 09:17 AM

Not entirely sure why you are getting that error, however you need to keep in mind that everything you put it in the resources folder will ALWAYS get added to your final build and therefore should be kept as lean as possible since if you end up not using said object and leave it in there your loading unnecessary information to your final build.

you could try this setup instead:

 public GameObject prefabModel; // drag into this position in inspector from your prefabs folder
 GameObject child;
 public bool test = false;  // in inspector make it true to test the destroy function.
 
 void Start(){
        
 child = p.transform.GetChild(0);
       
 }
 
 void Update(){
 
 if(test){
       Destroy(child);
 }
 }

Check if that works.

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 samulihe · Aug 23, 2017 at 09:36 AM 0
Share

Sorry I can't do it that way becauce the child will be instantiated at runtime dynamically from many different prefabs. ie. "path/to/model" will be different from time to time

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

94 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

Related Questions

Grabbing all Children and components of those children 1 Answer

Creating new Transform from existing objects Transform to Instantiate object 1 Answer

How can I get a parent GameObject of gameObject using Javascript? 6 Answers

Access a child from the parent or other gameObject. 2 Answers

instantiate an object without being the child of another 1 Answer


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