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 /
  • Help Room /
avatar image
0
Question by Archonius · Mar 19, 2016 at 12:56 AM · c#unity 5instantiatechildren

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

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
Best Answer

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.

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

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

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

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


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