i’m trying to make a code so that if my player hits an object with a Obstacle tag it would react and print a message in my console
using UnityEngine;
public class Collision : MonoBehaviour
{
void OnCollisionEnter(Collision collisionInfo)
{
if (collisionInfo.GetComponent<Collider>().tag == "Obstacle")![109109-capture.png|339x72](upload://tRZTpGWbdmIrbjKooAqdn4H43eA.png)
{
Debug.Log("We hit a obsicle");
}
}
}
Name “conflict” between your class name and the actual Collision class. On the OnCollisionEnter parameters the Collision refers to this class and not the Unity’s one thus and the error, change your class and file name, it will be ok or in case you don’t want to change it (bad choice though)
update your code to look like this
void OnCollisionEnter(UnityEngine.Collision collisionInfo)
In general avoid to use same names as Unity’s API.
Cheers!
First, you should rename your class and filename because Unity already has a Collision class.
Second, you don’t need to use CollisionInfo, it’s unnecessary and no need for GetComponent either.