Hi Guys,
I think i have a very simple problem. Basically i want a collision script. So when my player touches the spikes he dies. i have made a script but it just wont work, i am a beginner by the way. Please it would be nice if you help.
Thank You
using UnityEngine;
using System.Collections;
public class PlayerDie : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter(Collision collision){
if (collision.gameObject.name == "Spikes") {
Destroy(collision.gameObject);
}
}
}
Looks like you’re using Rigidbody2D. For some reason w/ the 2D stuff, I can only get collisions to work correctly if both objects included have Collider2D AND Rigidbody2D. You also need to use OnCollisionEnter2D, not OnCollisionEnter.
I am not sure from this code that if the player is the one with the PlayerDie Script, or if the spikes have the script, since the collision is expecting to be against “Spikes”, i will asume that the player has the script, then collission.gameobject will be the spikes, meaning that if the player collides with the spikes the object to be destroyed would be the spikes, if you would want the player to be destroyed use Destroy(gameObject); in case the script is attached to the player