- Home /
Respawn target after destroying returns error
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);
}
Answer by DiligentGear · Dec 29, 2013 at 12:06 PM
using SetActive() instead of Instantiate/Destroy.
Public GameObject initialSpawn; // Changed
Public GameObject respawn; // Changed
public static bool dead = false;
void Awake()
{
initialSpawn = Instantiate(respawn, new Vector3(0,7,0), Quaternion.identity) as GameObject; // Changed
initialSpawn.transform.parent = transform;
}
void Update () {
if (dead == true)
{
StartCoroutine(pauseBeforeReSpawn(2f));
dead = false;
}
}
void OnTriggerEnter(Collider c)
{
if(c.gameObject.name =="barrel" || c.gameObject.name == "ground")
{
initialSpawn.SetActive(false); // Changed
dead = true;
}
}
IEnumerator pauseBeforeReSpawn(float seconds)
{
yield return new WaitForSeconds(seconds);
initialSpawn.transform.position = new Vector3(4, 7, 0);
initialSpawn.SetActive(true); // Changed
yield return null;
}
I tried your code. In returns the same error message and multi spawns the object.
For the variable Transform respawn, I am making it public. I attach the above code to the object which I want to be respawned.
I also drag and drop the same object's prefab to the variable Transform respawn from unity Inspector. Am I doing anything wrong at these steps?
I just edited the code. It spawns multiple objects because I was using Start rather than Awake. I'm sorry for the mistake.
What error did you get? If you got something like "Object reference not set to an instance of an object", try to drag and drop again. Now it uses GameObject ins$$anonymous$$d of Transform.
UnassignedReferenceException: The variable respawn of 'RandomSpawn' has not been assigned. You probably need to assign the respawn variable of the RandomSpawn script in the inspector.
NullReferenceException RandomSpawn.OnTriggerEnter (UnityEngine.Collider c) (at Assets/_Script/RandomSpawn.cs:25)
Hi edited to your latest version and got the above 2 errors.
I dragged and dropped the object again for respawn variable. I changed the initialSpawn to private since the value is being assigned in the codes. I double checked and the names of the objects are correct.
The object is created once, collides with the barrel and program crashes. Thanks for the time you taking to help. :)
It's still complaining null reference. Program crashes since it doesn't know what "respawn" is. Did you make your respawn a Prefab?
You might want to try to add your "respawn" prefab into your scene, and add this line in Awake, before the instantiation:
initialSpawn = GameObject.Find("respawn"); // note that it's finding object by its name. $$anonymous$$atch it if you have different name.
And you can always do Debug.log(initialSpawn) to check whether it's null or not.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to respawn an enemy prefab after its destroyed 1 Answer