Collision “Can not convert type string to bool”

I am making a game following a brackeys tutorial.

Using UnityEngine;

public class PlayerCollision : MonoBehaviour

void OnCollisionEnter(Collision collisionInfo)
{

if (collisionInfo.collider.tag = “Obstacle”
{
    Debug.Log(“We hit a obstacle”);
}
}
}

I want it so that when I hit a object it tells me.

but it says “cannot turn string to bool”

How do I fix this

Hey, here are (hopefully all) the things wrong with your script:

  • Your class is missing an opening curley bracket. This should be added by default, so you probably deleted it accidentally.
  • Inside your if-statement you are trying to assign a new string as tag (which is causing the error), instead of comparing it. You probably meant to use == instead of = there.
  • Your if-statement is missing a closing bracket after the parameter.

Did you copy this code or retype it? Pretty sure there should be an error for the missing opening class bracket before anything else. Anyways, make sure you dont miss brackets.

Thx. But could you write the correct code

I gave you 3 pretty precise explanations of the 3 mistakes you made, all of which include only one missing character.
But here you go:

using UnityEngine; // You also wrote "Using" with a capital letter. I never tested that, but pretty sure it's incorrect. Proper spelling is highly important!

public class PlayerCollision : MonoBehaviour
{ // the opening bracket here

    void OnCollisionEnter(Collision collisionInfo)
    {
        // the equal sign here
        if (collisionInfo.collider.tag == “Obstacle”) //this bracket here
        {
            Debug.Log(“We hit a obstacle”);
        }
    }
}

Also, pay some attention to indentation.
The way i formatted it, you will likely agree, makes it way easier to spot missing brackets.

1 Like

Thanks