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 jdaviee · Oct 21, 2020 at 03:06 PM · instantiateprefab3dlocalposition

Instantiate Object At Local Position

This question is kind of continued from this one: https://answers.unity.com/questions/380829/instantiating-a-prefab-at-local-position.html

I can instantiate an object to local space and it works with the following code:

 private void AddGoalStalker()
     {
         GameObject goalStalker = Instantiate(goalStalkerPrefab, hazardDirectory, false) as GameObject;
         Vector3 pos = new Vector3(hole.transform.position.x, goalStalker.transform.position.y, hole.transform.position.z);
         goalStalker.transform.position = pos
     }

However, the below code doesn't work even though it's essentially doing the same thing?

 private void AddGoalStalker()
 {
     Vector3 pos = new Vector3(hole.transform.position.x, goalStalkerPrefab.transform.position.y, hole.transform.position.z);
     InstantiateObject(goalStalkerPrefab, hazardType.GoalStalker, pos);
 }

 private void InstantiateObject(GameObject prefab, Hazard hazard, Vector3 pos)
 {
     GameObject obj = Instantiate(prefab, hazardDirectory, false) as GameObject;
     float rot = RandomRot(hazard);
     obj.transform.position = new Vector3(pos.x, pos.y, pos.z);
     obj.transform.Rotate(Vector3.up, rot, Space.Self);
 }


Does anyone know why this doesn't work? I need to do it the second way for additional checks in the InstantiateObject() method that won't affect the positioning.

Any help will be really appreciated

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

2 Replies

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

Answer by unity_ek98vnTRplGj8Q · Oct 21, 2020 at 03:34 PM

goalStalkerPrefab.transform.position is not the same as goalStalker.transform.position. Try changing the line where you set the position to obj.transform.position = new Vector3(pos.x, obj.transform.position.y, pos.z);.


goalStalkerPrefab looks at the actual prefab object (probably has a y value of 0), while goalStalker.transform.position is looking at the copy of the prefab that you have spawned in your scene which (I believe) will have the same Y value as your "hazardDirectory" since you are spawning it as a child of that.

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
avatar image
0

Answer by jdaviee · Oct 21, 2020 at 03:36 PM

Thanks for the help @unity_ek98vnTRplGj8Q , the changes worked! Below is the new, working code :)

 private void AddGoalStalker()
 {
     Vector3 pos = new Vector3(hole.transform.position.x, goalStalkerPrefab.transform.position.y, hole.transform.position.z);
     InstantiateObject(goalStalkerPrefab, hazardType.GoalStalker, pos);
 }
 
 private void InstantiateObject(GameObject prefab, Hazard hazard, Vector3 pos)
 {
     GameObject obj = Instantiate(prefab, hazardDirectory, false) as GameObject;
     float rot = RandomRot(hazard);
     obj.transform.position = hazardDirectory.position + pos;
     obj.transform.Rotate(Vector3.up, rot, Space.Self);
 }

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

239 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Explosion at mouse 1 Answer

GameObject position and localPosition not changing in hiearchy, only in script. 0 Answers

How use local position of prefab for my instantiate ? 1 Answer

Collider.bounds position is wrong? 3 Answers

Changing localPosition gives no error, but doesnt change prefab localPosition 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