• 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 NerdRageStudios · Feb 27, 2015 at 09:59 PM · gameobjectinstantiatetransformtransform.parent

problems parenting a gameObject to another upon instantiating when same name exists

Hi all, I am having trouble getting my code to automatically parent objects when instantiated.

Basically, my levels are all generated through code. It automatically generates empty gameobjects for the levels and the rooms in each level, then parents objects upon instantiating them to the correct places.

Ex:

Level1 > Room1 > LevelTile1

Level2 > Room1 > LevelTile1

Level3 > Room1 > LevelTile1

Level4 > Room1 > LevelTile1

However, despite everyt$$anonymous$$ng working fine, my tiles are all appearing under Room1 in level1, thats because I have no way (yet) to work out w$$anonymous$$ch levels the rooms are under.

Here are some code snippets of how its working:

First create the Level

             emptyObject = new GameObject("Level" + tempLevelNumber);
             GameObject currentLevel = GameObject.Find("Level" + tempLevelNumber);

Then create the room and parent to the level

                 emptyObject = new GameObject("Room" + tempRoomNumber);
                 emptyObject.transform.parent = currentLevel.transform;

Then create tiles and parent to the room

                 GameObject currentRoom = GameObject.Find("Room" + tempRoomNumber);
                 GameObject LeftEnd = Instantiate(levelEndLeft[Random.Range(0, levelEndLeft.Length)], levelStartPoint, transform.rotation) as GameObject;
                 LeftEnd.transform.parent = currentRoom.transform;

And here is where it goes wrong, as I create in level 2, 3, 4 etc. they are all parenting to Rooms under level 1 as I cannot seperate out w$$anonymous$$ch rooms are under w$$anonymous$$ch level!

ie. All tiles for each room are under level 1, and all the other rooms in the further levels have no c$$anonymous$$ldren as it finds them first in level 1.

I hope that all makes sense!! Any ideas?

Comment
Add comment · Show 1
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 NerdRageStudios · Feb 27, 2015 at 10:00 PM 0
Share

Sorry I should have said the the heirachy creates just fine, I all levels and all rooms are present, its just that all the tiles are children of the rooms in level 1 only, as I cant define which level the room is under when parenting a tile

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by NerdRageStudios · Feb 27, 2015 at 10:42 PM

Found my own solution using strings to create the full path for GameObject.Find

                 GameObject currentRoom = GameObject.Find("Room" + tempRoomNumber);
                 string stringLevel = ""+currentLevel.gameObject.name;
                 string stringRoom = "/" + currentRoom.gameObject.name;
                 string stringFullPath = stringLevel + stringRoom;
                 GameObject currentRoomInLevel = GameObject.Find(stringFullPath);
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

19 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

Related Questions

The Weirdest Bug Ever: GameObject Transform Appears in Editor to Be in One Spot but Instantiates and Registers in Another 2 Answers

How to instanciate transform to element of a list?,How Get Transform of a instanciated Prefap 3 Answers

Set parent of instantiated object. 0 Answers

Rotating a Vector3 in Instantiation 1 Answer

parent.transform creates new game object 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