- Home /
Instantiate ignores position
This was working until I updated unity to 2017.3.0f3 now Instantiate just ignores whatever position I enter and only instantiates at the prefabs location. I can put any trivial vector value in the position property and it simply does not change the objects position. If I add the line:
knife.transform.position = firePosition
It puts the object in the correct place. Is this a known issue or did something change in the Instantiate function?
Example code is below
void ThrowKnife(float stickAngle, Vector2 forceVector)
{
Bounds bounds = GetComponent<Collider2D>().bounds;
Vector2 firePosition;
float yPos = bounds.center.y;
if (FacingRight)
{
Debug.Log("Facing Right");
firePosition = new Vector2(bounds.max.x + .1f, yPos);
}
else
{
firePosition = new Vector2(bounds.min.x + .1f, yPos);
}
GameObject knife = Instantiate(Knife, firePosition, new Quaternion(), transform.parent);
knife.transform.eulerAngles = new Vector3(0, 0, stickAngle);
knife.GetComponent<Knife>().ForceAngle = forceVector * ThrowSpeed;
}
Never, ever use new Quaternion()
this will not create a valid unit Quaternion as all value types when using the default constructor are initialized with the default values of it's components. That means it's equivalent to new Quaternion(0,0,0,0)
which is not a valid unit quaterion. If you want to specify the identity rotation use Quaternion.identity
or new Quaternion(0,0,0,1)
which would be the same.
Answer by Harinezumi · Jan 28, 2018 at 07:55 PM
I haven't used this overload of Instantiate()
, but I'm guessing that specifying the parent will modify the position and rotation. As you are also assigning a specific angle after, why don't you do it like this?
GameObject knife = Instantiate(Knife);
knife.transform.SetParent(transform.parent);
knife.transform.position = firePosition;
knife.transform.eulerAngles = new Vector3(0, 0, stickAngle);
Another option is:
GameObject knife = Instantiate(Knife, firePosition, Quaternion.Euler(0, 0, stickAngle));
knife.transform.SetParent(transform.parent, true);
Answer by BrokawBound · Mar 21, 2018 at 06:48 PM
Hello, I am having the same problem after updating to the same version. I was using the 2017.0 and upgraded to 2017.3.
All my instantiated objects used the position set in the pre-fab instead of the position I requested in the function.
I found that in 2017.3 you can no longer set the position at the same time that you instantiate. You must set the position AFTER. So just add this line to your code right after instantiating, it does not appear to cause any screen flickers but it would be good to know why this was changed.
knife.transform.position = firePosition;
This is what i'm experiencing too. It makes no sense for unity to change it. Quite possibly a bug. Have you reported it yet?
Your answer
![](https://koobas.hobune.stream/wayback/20220612151456im_/https://answers.unity.com/themes/thub/images/avi.jpg)