Reputation: 3651
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);
}
This is a separate class that is killing the object:
public class PlayerManager : MonoBehaviour {
void Update () {
}
void OnTriggerEnter(Collider enter)
{
if (enter.collider.gameObject.CompareTag("fruit"))
{
ScoreCounter.counter += 10;
Destroy(enter.gameObject);
}
}
}
Upvotes: 0
Views: 534
Reputation: 2381
try this:
void OnTriggerEnter(Collider c)
{
if(!dead && (c.gameObject.name =="barrel" || c.gameObject.name == "ground"))
{
Destroy(initialSpawn);
dead = true;
}
}
I'd couple this with Alex's changes, because I don't much like the look of the Coroutine in there.
Upvotes: 1
Reputation: 1109
Some problems I noticed:
The coroutine placement is wrong and does nothing. Or at least it doesn't do what you want it to do.
Destroy()
doesn't destroy objects in the same frame. So your Update
may get screwed up because the object is about to be destroyed yet you are still trying to change what the variable initialSpawn
is referencing.
public static bool dead = false;
This will cause all instances of this object to die horribly if you ever have more than one. You should probably consider removing static
keyword.
I think solving the first issue will also solve the second. Here is what you could try :
public float respawnTimeOut = 2F;
public static bool dead = false;
private float timeSinceDeath = 0F;
private Transform initialSpawn;
private Transform respawn;
void Start()
{
initialSpawn = Instantiate(respawn, new Vector3(0,7,0), Quaternion.identity) as Transform;
initialSpawn.parent = transform;
}
void Update ()
{
if (dead)
{
if(timeSinceDeath >= respawnTimeOut)
{
initialSpawn = Instantiate(respawn, new Vector3(4, 7, 0), Quaternion.identity) as Transform;
initialSpawn.parent = transform;
dead = false;
timeSinceDeath = 0F;
Debug.Log("test working");
}
else
{
// Update death timer
timeSinceDeath += Time.deltaTime;
}
}
}
void OnTriggerEnter(Collider c)
{
if(c.gameObject.name =="barrel" || c.gameObject.name == "ground")
{
Destroy(initialSpawn);
dead = true;
}
}
Upvotes: 1