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 )