Specify rotation of instantiated object

Hey there,

How do I specify the rotation of an object I am instantiating?

I am using the following code:

Instantiate (gameObjectName, Vector3(xVal, yVal, zVal), Quaternion.identity);

But they always instantiate 'backwards', I have tried transform.rotation, gameObjectName.transform.rotation .etc and they all yield the same results.

They are 'backwards' because the Prefab you use is rotated that way.

Try to use localRotation:

transform.localRotation = Quaternion.identity;

Or fix the rotation in your Prefab

It might have to do with how you're exporting and importing the model. You want to make sure to freeze the transform on the object in your 3D package before exporting it, so that when the rotation is all 0's it's facing the direction you expect it to.

If the object you're instantiating is facing the correct way and its rotation is zeros, make sure it's not parented to an object that is rotated. This goes back to the above, where two wrongs can appear to make a right.

If all that is good so far, then `gameObjectName.transform.rotation` should work. If you're not sure what's going on, then what you could do is keep a reference to the newly instantiated object and tell it to look at a certain direction you want it to face. For example:

var target: GameObject;
// set the target in the inspector
var newObj: GameObject = Instantiate (gameObjectName, Vector3(xVal, yVal, zVal), Quaternion.Identity);

If after this, for example, your new object is 90 degrees left, then you should go back to your 3D software and rotate the object 90 degrees to the right, freeze the transform, then re-export/import.