Collision unity parameters issue

I am new to both unity and programming. I have been following this tutorial How to make a Video Game in Unity - COLLISION (E05) - YouTube

My code is the same as his but mine is not working. Would you mind helping me out pls? Also my Microsoft visual looks different and does not give me the shortcuts when I type like his. I am attaching the screenshots. His is the orange font one.

,I have been following this tutorial on yt. I am just getting started so I do not much about coding and about the software in general. But my script doesn’t work and his is working. Even the look of his Microsoft visual is different than mine. Can someone please help? I will attach the screenshots. The orange font one it’s his.

It’s just a few simple syntax errors. This should fix it:

private void OnCollisionEnter(Collision collisionInfo)
{
    if(collisionInfo.gameObject.GetComponent<Collider>().name == "Obstacle")
    {
        Debug.Log("We hit an obstacle!");
    }
}

You used this before:

private void OnCollisionEnter(Collision collisionInfo)
{
 if(collisionInfo.GetComponent<Collider>().name == "Obstacle")
{
    Debug.Log("We hit an obstacle!");
}
}

The difference is the spacing. When using { and } you need to move everything in between by four spaces(or one press on the tab button on your keyboard). Also you will need to do collisionInfo.gameObject.GetComponent. That is because the collisionInfo is the collider you hit. Then you typer .gameObject for the script to get the GameObject the collider is attached to. If you just type collisionInfo.GetComponent the script is trying to get a component attached to the Collider component instead of the GameObject.

I hope it makes sense and that it helps you.

  • Axel