When prefab instantiates it dosent have wright rotation
using UnityEngine; using System.Collections;
public class spawning : MonoBehaviour {
public GameObject enemy;
// Use this for initialization
void Start () {
StartCoroutine (spawningg ());
}
// Update is called once per frame
void Update () {
}
IEnumerator spawningg()
{
while (true) {
float second = Random.Range (2,4);
yield return new WaitForSeconds (second);
Instantiate (enemy, transform.position, Quaternion.identity);
// Debug.Log (second);
}
}
}
This code instantiates object within 2 second but problem is that when it instantiates it has rotation of 0,0,0 but prefab has set rotation of 0,0,180 so it looks down , when object is instantiated it dosent rotate Thans in advance for help.
Answer by Mmmpies · Jun 07, 2016 at 07:49 PM
When you instantiate it try this...
GameObject go = Instantiate (myPrefab, transform.position, Quaternion.identity) as GameObject;
go.transform.rotation = Quaternion.Euler (0, 0, 180);
identity is the real world 0 for rotation so you're not accessing the, not yet instantiated, object to set the rotation. It can't till it exists and the same for transform.position which is your spawn point.
So keep a local reference for the gameObject and set its rotation once you've instantiated it.
This is also handy for adding components like scripts. You can even add a script and set a public variable in that script this way. Which is very handy if you want to individually tag each enemy.