Hi there, So i’m trying to increase the score when an object with a certain tag is destroyed, all seems to be working except for this one error which i cant work out. Still very new to programming so its hard to spot my mistakes.
(12,17): error CS0029: Cannot implicitly convert type string' to bool’
using UnityEngine;
using System.Collections;
public class CubeManager : MonoBehaviour {
public int score = 1;
// Use this for initialization
void OnMouseDown () {
Debug.Log ("clicked");
Destroy (this.gameObject);
if (this.gameObject.tag = "Blue")
score += 1;
}
void OnGUI () {
GUI.Label (new Rect (2, 2, 100, 20), score.ToString());
}
}
Only new to Unity but I think your problem is here:
if (this.gameObject.tag = "Blue")
First, generally in other languages, you use == intead of = to compare things. Not sure if that’s the case here, but one to watch for regardless. I suspect this is the main problem, as the single = makes it an assignment.
So, what happens is you are SETTING the tag to “Blue”, and then trying to if() this assignment. It looks like Unity / C# might return the string after an assignment, letting us do other cool tricks, but the end result is that your line changes the tag, not compare them.
Two, destroying it then trying to access a tag on it seems like a recipe for disaster. Put destroy AFTER the check.
Third, in most languages, you compare strings with a different function, eg, in Java,
1: testString = "Bob";
2: if (testString == "Bob");
3: if (testString.equals("Bob"));
Hey there, don’t worry about being new, we all started somewhere.
As Eric5h5 said, you don’t need to use the ‘this’ keyword, since ‘this’ is implied already.
To fix your error, you’re using the assignment operator instead of comparison. “=” assigns the container before the symbol with the content of the code after it. “==” is a comparison.