- Home /
Why can't I make my character die?
Trying to respawn my character when he hits the lava. Not sure why it's not working. There aren't any errors. It's just not doing anything.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Death : MonoBehaviour {
public Vector3 RespawnPoint;
public Transform player;
// Start is called before the first frame update void Start() { RespawnPoint = new Vector3(0, 0, 0); }
// Update is called once per frame void Update() {
}
private void OnCollisionEnter(Collision collision) { if (collision.gameObject.tag == "Lava") { Debug.Log("Collision"); Reset(); } }
public void Reset() { player.position = RespawnPoint; } }
Answer by TooManyPixelz · Feb 07, 2020 at 11:15 PM
The script didn't really work right, so I'm going to put it here. https://hatebin.com/tjvffqutyz
Answer by rh_galaxy · Feb 07, 2020 at 11:27 PM
I think you are trying to compare Lava to your player object, this could be the way to do it
string szOtherObject = collision.collider.gameObject.tag;
if(szOtherObject.CompareTo("Lava") == 0)
{
Debug.Log("Collision");
}
What do you mean by comparing Lava to my player object? Also, why am I setting that to 0?
There are two objects in a collision, collision.gameObject is the one that collided (player?), collision.collider.gameObject is the other (and you want to know if the other is "Lava").
comparing to 0 is just the way CompareTo works, nothing is set to 0.
And also, it still doesn't work. Any other ideas?
Answer by duanegra · Feb 08, 2020 at 01:28 AM
Your script is set up to apply to another game object. I updated it to attach directly to your player, no need for public variables. I tested and it works!
Here is the update:
// Attach script to a player, or NPC
Vector3 RespawnPoint;
void Start () {
// Sets the respawn point to the position where the player started
RespawnPoint = gameObject.transform.position;
}
void OnCollisionEnter (Collision collision) {
// detects if Game Object with a collider is tagged "Lava"
if (collision.gameObject.tag == "Lava") {
Debug.Log ("Collision");
Reset ();
}
}
void Reset () {
// send player back to starting point
gameObject.transform.position = RespawnPoint;
}