- Home /
Unity error CS0029: Cannot implicitly convert type `int' to `bool' - help?
Unity error CS0029: Cannot implicitly convert type int' to
bool' I keep getting this error while trying to add a animation trigger to my C# script:
using UnityEngine;
using System.Collections;
public class Score : MonoBehaviour {
static int score = 0;
static int highScore = 0;
Animator animator;
static public void AddPoint() {
score++;
if (score > highScore) {
highScore = score;
}
}
void Start() {
score = 0;
highScore = PlayerPrefs.GetInt ("highscore", 0); {
if (highScore = 20)
animator.SetTrigger ("Char2");
}
}
void OnDestroy() {
PlayerPrefs.SetInt ("highscore", highScore);
}
void Update () {
guiText.text = "Score: " + score + "\nHighscore: " + highScore;
}
}
What i have here is a score system and i'm trying to make it so when the highScore is 20, it triggers an animation. But this line:
if (highScore = 20)
animator.SetTrigger ("Char2");
Keeps giving me the Unity error CS0029: Cannot implicitly convert type int' to
bool' I've looked around for a solution but it's confusing me further (I'm pretty new to Unity and C# so bare with me)
So my question is, how do i go about making this thing work so that when the highScore reaches 20, the animation is triggered?
Answer by Andres-Fernandez · May 07, 2014 at 08:49 AM
You are missing one equals sign. Line should be:
if (highScore == 20)
That doesn't seem to fix the problem, it just gives me a different error which says:
CS0649: Field Score.animator' is never assigned to, and will always have its default value
null'
Well, it does fix the problem you had (the actual question you posted).
What you have here is another problem. As the error says, you don't assign any value to the animator, so it'll never trigger any animation. Try setting it as public and link it in the editor with the game object whose animator you want to work with.
Once you have it linked, you can play any state or whatever you want to do with it.
How exactly do i go about linking it with the game object? (Sorry if that's a dumb question, still learning some things)
And thank you for the response!
After making animator a public field. In unity select your score object. In the inspector there should now be new field "animator" drag the animation you want to use in to it.
Thanks for the response. It seems like every time i fix one error, another pops up
error CS0120: An object reference is required to access non-static member `Score.animator'
Do i need to make it a static public, or how do i add an object reference?