Cannot implicitly convert type string to bool

Okay I was doing a tutorial and I was writing some collider script and everything seems fine but I keep getting this message that states cannot implicitly convert type string to bool

using UnityEngine;
using System.Collections;

public class DestroyByContact : MonoBehaviour
{
    void OnTriggerEnter(Collider other) {
        if (other.tag = "Boundary")
        {
            return;
        }
               
        Destroy (other.gameObject);
        Destroy (gameObject);
    }

}

Assets/Scripts/DestroyByContact.cs(7,17): error CS0029: Cannot implicitly convert type string' to bool’

== Compares not =

1 Like

??? Could you be a bit more clear please

[LIST=1]
[*]using UnityEngine;
[*]using System.Collections;
[*]

[*]public class DestroyByContact : MonoBehaviour
[*]{
[*]   void OnTriggerEnter(Collider other) {
[*]       if (other.tag == "Boundary")
[*]       {
[*]           return;
[*]       }
[*]              
[*]       Destroy (other.gameObject);
[*]       Destroy (gameObject);
[*]   }
[*]

[*]}
[/LIST]

Can’t be bothered editing out the List code tags sorry

do I add all those in?

EDIT nvm I figured it out

Just to be clear. In c# = is assignment. So your original code if (other.tag = “Boundary” ) is assigning “Boundary” to other.tag And since this doesn’t return a true/false if is yelling at you.

== is the comparison operator. This returns true or false , if the two things are equal to each other.

So
x= 5; Make the variable x take the value of 5
x== 5 Ask if x is 5 and return true/false if it is, or is not.

3 Likes

so how u do it i have same error.

a good way to improve your programming skillset is to read the error message thoroughly. the error is stating that you cannot convert STRING to BOOL.

so you should then think… ok, im clearly trying to set a string value to a boolean value
now, find… in your code, where you’re setting String = Bool.

A little hint for you; in programming
= is used for setting
== is used for comparison
=== is used for precision comparison (1 === 1) will return true; but (1 === 1.0) will return false. reason: left side is integer, right side is double.

using UnityEngine;

public class playerposition : MonoBehaviour{

void OnCollisionEnter(Collision collisionInfo)
{
if (collisionInfo.collider.tag = “Obstacle”)
{
Debug.Log(“We hit an obstacle!”);
}

}
}

it says that cannot convert STRING to BOOL.
please help mee

Some points:

  • Do not necro post or try to hijack other threads. Create your own thread so we can address your problem
  • When you post code, use code tags. There’s a sticky post in the scripting forum right at the top that explains how.
  • Did you actually read through this thread and what people have answered? You made the same mistake as the OP. So the post #2 applies to you just the same way.
2 Likes

Please create a new thread and explain in detail the issues you are having.
Thread locked

1 Like