Referencing the Transform of the children of an Instantiated GameObject _PLEASE HELP!!!
Firstly, I'm going mad with this. Sorry the problem is long but I'd really appreciate some feedback! I've been trawling the internet and tried countless things and everything I do seems to return null.
Basically I have a spaceship split in to 4 parts; Hull, Engine, Cockpit and Wings (from henceforth referred to as ship modules). I have multiple variations of each part so the player can upgrade individual parts and I can simply destroy the old GameObject and instantiate the new part in. The Hull prefabs all have empty GameObjects as children that I have snapped perfectly to the position on the hull where the ship modules join. Each different type of hull has it's own unique set of children as the positions of the ship modules changes slightly as the hull model changes size. This is so I can use these empty GameObject Transform values to position the ship modules when I instantiate them in.
private GameObject currentHull;
public void SpawnModules(Vector3 position, Quaternion rotation, int hull, int cockpit, int engine, int wings)
{
currentHull = (GameObject) Instantiate(hulls[hull], position, rotation);
So I'm calling the method and telling it where to place the hull and what level the ship modules should be. I'm instantiating the hull in to the currentHull Variable. Now I want to instantiate the remaining modules around the hull and can't find a way to reference the currentHull children to get their positions! I have tried using their tags:
Transform engineTransform = GameObject.FindGameObjectWithTag("Engine Transform").transform;
And it returns null. I tried finding the object in the children:
Transform engineTransform = currentHull.GetComponentInChildren<Transform>().Find("Engine_Spawner");
And it returns null. I tried having a script on the hull prefabs them self that collects the transforms of it's children:
public class AddModuleTransforms : MonoBehaviour
{
public Transform engineTransform;
public Transform cockpitTransform;
public Transform rightWingTransform;
public Transform leftWingTransform;
}
Then using this to grab them from the other class:
Transform engineTransform = currentHull.GetComponent<AddModuleTransforms>().engineTransform;
Guess what? Returns null! After a day on this issue I'm out of ideas.
Basically I want to get to this:
currentEngine = (GameObject) Instantiate(engines[engine], engineTransform.position, engineTransform.rotation);
currentCockpit = (GameObject) Instantiate(cockpits[cockpit], cockpitTransform.position, cockpitTransform.rotation);
currentRightWing = (GameObject) Instantiate(rightWings[wings], rightWingTransform.position, rightWingTransform.rotation);
currentLeftWing = (GameObject) Instantiate(leftWings[wings], leftWingTransform.position, leftWingTransform.rotation);
But I can't find a way of referencing the children of the instantiated hull!!
Any help is appreciated! Thanks
Rob
Answer by Archonius · Mar 19, 2016 at 12:16 PM
Okay.. So it turned out I had dragged the Hull Models in to the hull modules array not the Hull prefabs that have the children. This revelation literally came to me while unable to sleep after spending a day on this problem! ALL of the above solutions I listed work perfectly so I hope this post can at least help other people. All of the methods listed work for finding the children of an Instantiated object.
Your answer
![](https://koobas.hobune.stream/wayback/20220612072419im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
how do i remember a just instantiated prefab as a variable gameobject? 1 Answer
I'm having a few issues with (Catmull)Splines that i need help with. 1 Answer
How do I Instantiate a prefab at the x axis position of where the UI button was clicked 1 Answer
Problem with Instantiate custom class 0 Answers
Problem with projectile direction 0 Answers