- Home /
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;
try to reset the position of the prefab with
theflash.transform.parent = Camera.main.transform;
theflash.transform.position = new Vector3(0,0,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.
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?
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.
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!
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.
Your answer