Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by jfcernel · Jan 23, 2021 at 11:23 PM · instantiatetransformchild object

Issue with local and world positioning

I have looked at a lot of other posts about local vs global positioning and how everyone is saying that transform.position is supposed to return the global position. Somehow I'm having an issue with that and I'm not sure how.


Here is part of the code below. This part works just fine. I have some empty game objects that I'm using as placeholders to spawn different rooms randomly. Those templates are positioned in world space correctly and when I spawn a random room, it is in the position I expect it to be.


The issue I'm having is with a child object of the room itself. I have a script called "Room Properties" and that script has a few variables, the one in question is the "suspect spawn" which is just a GameObject.


I'm adding that gameObject to a List so that I can do something with it later. However, it seems the position of it is referenced in global space, not in reference to its parent prefab.


For example, if the spawn was (-2, 0, 0) from the parent of the prefab, so just a bit off center from the prefab, when I'm trying to instantiate objects at that location, it is instead spawning them in the global context and they are spawning roughly on top of each other.

 foreach(GameObject template in mediumRoomsTemplates)
         {
             int rand = Random.Range(0, potentialOptions.mediumRoomPrefabs.Length);
             GameObject choice = potentialOptions.mediumRoomPrefabs[rand];
 
             Instantiate(choice, template.transform);
             //GameObject.Destroy(template);
 
             RoomProperties prop = choice.GetComponent<RoomProperties>();
             characterSpawns.Add(prop.suspectSpawn.gameObject);
             
         }



The part I don't understand is this code. If I use the characterSpawnPoints array that I got from gameObjectWithTag, they will spawn in the correct position, however, if I use the list, characterSpawns which was the list that I was pushing those prefab child object spawn points to, the position is always based on the global (0,0,0) so the characters overlap each other. Those should be the same gameObjects, so I don't understand why the position is different.

   GameObject[] characterSpawnPoints = GameObject.FindGameObjectsWithTag("Character Spawn Point");
   
         int counter = 0;
         foreach (GameObject suspect in sus)
         {
             GameObject spot = characterSpawns[counter];
 
             Instantiate(suspect, spot.transform.position, spot.transform.rotation);
             counter++;
         }

Can someone explain to me why I'm getting different positions? I assume it's somehow related to how I'm adding those child spawns to the list, but I'm really not sure what I'm doing wrong.

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 jfcernel · Jan 24, 2021 at 12:11 AM

Shortly after posting, I realized what the solution is. Since I build my prefabs at (0,0,0), this is why I am seeing it behave as it is. When I instantiate the room, I need to actually return a clone of that object that is created. This way I am actually getting the script off the object that is now positioned in the world space and therefore the referenced object is no longer referencing the prefab's original positioning. Posting the solution in case anyone else has the issue.

             GameObject clone = Instantiate(choice, template.transform);
             //GameObject.Destroy(template);
 
             RoomProperties prop = clone.GetComponent<RoomProperties>();
             characterSpawns.Add(prop.suspectSpawn.gameObject);
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

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

Instantiating a new gameObject as a child of a different gameObject 2 Answers

Getting instance of an sub object rather than the original's subobject 0 Answers

Not your typical "Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption" 0 Answers

how to make a Portal 2-esque laser beam "redirection" system? 0 Answers

Instantiate Terrain Object as child of Empty 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