I am using unity3d c# to respawn an object after destroying it at a specific position. The following code returns an error message:
Quarternion to matrix conversion failed because input quaternion is invalid
I get multiple spawns of the prefab a couple of seconds followed by the error message and they spawn all over the screen.
My codes as follows. For the variable respawn, I dragged and dropped the prefab from Unity itself. Thanks for any guidance.
Transform initialSpawn;
Transform respawn;
public static bool dead = false;
void Start()
{
initialSpawn = Instantiate(respawn, new Vector3(0,7,0), Quaternion.identity) as Transform;
initialSpawn.parent = transform;
}
void Update () {
if (dead == true)
{
//The line below is the one giving the error
initialSpawn = Instantiate(respawn, new Vector3(4, 7, 0), Quaternion.identity) as Transform;
initialSpawn.parent = transform;
StartCoroutine(pauseBeforeReSpawn(2));
dead = false;
Debug.Log("test working");
}
void OnTriggerEnter(Collider c)
{
if(c.gameObject.name =="barrel" || c.gameObject.name == "ground")
{
Destroy(initialSpawn);
dead = true;
}
}
IEnumerator pauseBeforeReSpawn(int seconds)
{
yield return new WaitForSeconds(seconds);
}