- Home /
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 )
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.
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
Follow this Question
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