So I am creating a 2d platformer, and currently I am attempting to get a script to respawn the character when he falls of a platform. I have a box collider below and when the character passes through he is supposed to respawn at (0,0,0), but nothing is happening.
using UnityEngine;
using System.Collections;
public class DeathDetection : MonoBehaviour {
public GameObject player;
private void OnTriggerEnter(Collider collider){
if (collider.tag == "Player") {
player.transform.position = Vector3.zero;
}
else {
Destroy(collider.gameObject);
}
}
}
It doesn’t look to me like you’re actually trying to “respawn” anything — you’re either moving the player, or destroying the object. Just want to be sure I understand properly.
So when you say “nothing is happening” what do you mean? The player is destroyed? Or he just falls right through the collider without any effect?
If he’s destroyed, then I’d say his collider is not tagged the way you think it is. If he falls right through, then maybe he doesn’t have a collider, or maybe you haven’t assigned the player object to the player property of this script.
I suggest either busting out the MonoDevelop debugger, or if that seems daunting, then sprinkle some Debug.Log calls liberally into the above code so you can see what’s going on.
Thanks for the reply Joe. I was just going to start out basic and respawn the player or move his position back to the start which is (0,0,0). The player is just falling through the collider without any effect whatsoever. The player does have a collider, a box collider and I have assigned the player to the object property of the script. I was thinking about putting some Debug.Log, but I don’t really know how to go about that.
Edit: Ahh I believe its because I dont have a rigidbody on him, but I do have a collider. Even when I put a rigidbody on him the collider just destroys him doesnt change his position