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 gorgoroth · Dec 11, 2013 at 02:59 PM · instantiateposition

Instantiated Prefab wrong position

I am trying to instantiate a prefab whose position is set to be in front of the player as a parent of the main camera. This has worked for other gun prefabs, but for a certain set of prefabs (instantiated in the exact same way), they instantiate at positions completely different from where they are supposed to (i.e. instead of 1.0, -951.3 or something for x). I am puzzled to say the least, and I am hoping someone could suggest a solution.

This is how they are instantiated:

 GameObject flash = Resources.Load("GunFlashMagnum") as GameObject;
     
             theflash = GameObject.Instantiate(flash) as GameObject;
             theflash.transform.parent = Camera.main.transform;
 
 //where earlier in the script...
 GameObject theflash;
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 Vandarthul · Dec 11, 2013 at 03:40 PM 0
Share

try to reset the position of the prefab with

 theflash.transform.parent = Camera.main.transform;
 theflash.transform.position = new Vector3(0,0,0);

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by guitarxe · Dec 11, 2013 at 03:39 PM

It's hard to say without some debugging, but reading a bit on the script reference brings up this tidbit of information that might be relevant to your problem:

When you clone a GameObject or Component, all child objects and components will also be cloned with their properties set like those of the original object. However, the parent of the new object will be null, so it will not be a "sibling" of the original.

So since you are using theflash.transform.parent rather than theflash.transform, it could be giving all sorts of problems if there is no parent.

Why don't you try setting the transform on the object itself, and not the parent?

You can also set the transform position at the same time as the object is being instantiated using this constructor:

static Object Instantiate(Object original, Vector3 position, Quaternion rotation);

Use the camera's transform and rotation for the above.

Comment
Add comment · Show 1 · 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 gorgoroth · Dec 13, 2013 at 01:59 PM 0
Share

Getting rid of the transform.parent part did allow me to use transform.position to change the position, but how do I set the position and rotation to a point in relation to the camera?

avatar image
0

Answer by yattipong · Oct 01, 2016 at 04:28 PM

(try to check this) may be your prefab has animator component that control position attached.

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 yonoodle · Nov 12, 2016 at 09:19 AM

I just run into similar problem with wrong instantiate position , might not be the same issue as yours ,

I checked all hierachy are set to (0,0,0) and also all the animation begin with (0,0,0)

that solved my problem , which caused by editing the animation at non origin(0,0,0) position and the animator saved the displacement value

besure you click "apply change to Prefab" from "GameObject" menu after changing all position to 0,0,0

good luck!

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 Casy10 · May 11, 2017 at 02:13 PM

Check if all the children of the prefabs are at (0,0,0) position. This worked for me. At first i checked only the parent prefab and it was on (0,0,0), but never checked for children. After some time i checked the children and they were at awkward positions. After i modified them it all worked perfectly. When you create a prefab ALWAYS verify each element of the prefab to assure it has the right position. You don't want to modify them after the prefab has already instantiated. That is just a pain in the ass.

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

21 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

Related Questions

Not instantiating prefab in the right place after 2017 update 2 Answers

Keep instantiated GameObject's position while parenting it to another GameObject 3 Answers

Instantiated object coordinates from code aren't the same as the coordinates from Inspector?,Inspector Coordinates not the same as GameObject.Find Coordinates? 1 Answer

How to instantiate the prefabs on grid in Unity? 1 Answer

Adjacent Placement of Objects according to varying height 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