Hello, i have a canvas with a Game over menu on it and i want it to enable after the player has collided with an obstacle. I dont know how to do it, thats why im here. If you could provide a sample code that would be great, thank you.`
using UnityEngine;
public class PlayerCollision : MonoBehaviour {
public playermovement movement;
public GameObject GameOver;
void Start()
{
GameOver.SetActive(false);
}
void OnCollisionEnter(Collision collisionInfo)
{
if (collisionInfo.collider.tag == "Obstacle")
{
movement.enabled = false;
GameOver.SetActive(true);
}
}
}`
Hi, make sure that your player have a Rigidbody.
I made this script, I hope this could help you:
public GameObject player; //The object with the "playermovement"
playermovement script; //The playermovement script
public GameObject gameOver; //The object "Game Over" of the Canvas
void Start ()
{
gameOver.SetActive(false);
script = player.GetComponent<playermovement>();
}
void OnCollisionEnter (Collision col)
{
if(col.gameObject.tag == "Obstacle")
{
script.enabled = false;
gameOver.SetActive(true);
}
}
}
Agree with @robrtoprz
Check collision table at bottom of this page to make sure your collision happens: Unity - Manual: Introduction to collision