- Home /
Could you help me with my respawn script c# 2d engine
Hi I'm having trouble respawning my player after he dies. It shows him die and the animation works fine but I can't get him to respawn afterwords. I have box colliders, both as colliders and as triggers, I even made the trigger colliders slightly bigger and still nothing happens. Any help would be appreciated. Here is my code for the death.
using UnityEngine; using System.Collections;
public class DeathScript : MonoBehaviour {
void OnCollisionEnter2D(Collision2D collision){
if (collision.gameObject.name == "Big_Asteroid" && collision.gameObject.name =="Medium_Asteroid") {
Debug.LogError("HIT!!!!!!!!");
StartCoroutine(DieAndRespawn());
}
}
IEnumerator DieAndRespawn() {
renderer.enabled = false;
yield return new WaitForSeconds(2.0f);
transform.position = new Vector3(0.0f, 0.0f, -5.0f);
transform.rotation = Quaternion.identity;
renderer.enabled = true;
}
}
Is the name of the colliding object both "Big_Asteroid" and "$$anonymous$$edium_Asteroid" or is it supposed to be one or the other? Because in that case, I would change the and (&&) to or (||).
If this isn't the issue, then I would like to know what is happening after your player dies. Does it print the log from the code above? Does it run the coroutine? if so, is the player you're trying to respawn, placed in 0,0,-5 like it is supposed to?
Your answer
Follow this Question
Related Questions
Randomly Generated Objects 1 Answer
Using "ouside" functions inside Unity? 2 Answers
error CS8025: Parsing error in C# code 3 Answers
Problems with instantiation... 1 Answer
Structs in C# Question 3 Answers