1
\$\begingroup\$

In Unity's documentation you can find an example usage of Instantiate:

https://docs.unity3d.com/ScriptReference/Object.Instantiate.html

using UnityEngine;

public class InstantiateExample : MonoBehaviour
{
    public GameObject prefab;

    void Start()
    {
        for (int i = 0; i < 10; i++)
            Instantiate(prefab, new Vector3(i * 2.0f, 0, 0), Quaternion.identity);
    }
}

Why do they add the "Quaternion.identity" instead of leaving it blank? Wouldn't the engine instantiate the object with the original object's rotation anyway?

\$\endgroup\$

4 Answers 4

2
\$\begingroup\$

Presumably because it is better to be explicit about it rather than implicit.

For example if I saw an Instantiate method that did not take a parameter for Rotation I might assume that this Object could not rotate. This way it is explicit that it can indeed rotate and it is up to the user to specify its rotation and indeed you do say how much you want it to be rotated by, namely the quaternion identity.

It basically comes down to software design and showing a methods contract with its parameters rather than having to look it up in the documentation. In practice though, yes they could have supplied an overload that did not take a rotation but that would add unnecessary bloating to the API.

\$\endgroup\$
4
  • \$\begingroup\$ This answer is actually incorrect. It does not do it purely for design decision. \$\endgroup\$
    – Gnemlock
    May 12, 2017 at 4:27
  • \$\begingroup\$ @Gnemlock Care to elaborate? Why else does it do it? \$\endgroup\$
    – dVyper
    May 25, 2017 at 21:06
  • \$\begingroup\$ @dVyper, because it won't assume a zero rotation. This is already stated in an alternate answer. \$\endgroup\$
    – Gnemlock
    May 25, 2017 at 22:18
  • \$\begingroup\$ @Gnemlock. Very good. It's always good to be clear to make it easy for beginners to understand. \$\endgroup\$
    – dVyper
    May 30, 2017 at 9:05
4
\$\begingroup\$

Well It does instantiate object as it is if you do not provide second parameter i.e information regarding the rotation.

Setting the Quaternion to Quaternion.identity will effectively set its euler rotation to (0 , 0 , 0) or no rotation. Otherwise the instantiated object will have same rotation as of prefab.Please Check Again

so if you write

tranform.rotation = Quaternion.identity;

all x,y,z rotation will reset to zero.

\$\endgroup\$
3
\$\begingroup\$

Reading the doc, we can see that if we want to use a position (vector3) in the second parameter, we need to supply also a quaternion as the 3rd parameter.

\$\endgroup\$
-1
\$\begingroup\$

Probably at the Unity developer's view, if position is changed, then rotation has a high possibility to be changed.

So they prefer this way. Anyway

Instantiate(prefab, new Vector3(i * 2.0f, 0, 0), prefab.transform.rotation);

Still make it no harder than leave it blank.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .