- Home /
GetComponent, int error, if statement, problem.
Hello,
I have a score text, child of a canvas. That text is ingame projected on a 3D cube. I want to make the cube bigger if the text >= 10 and if the text >= 100, so that the text still fits the cube.
I am really noob with this. This is my code. I am placing this script onto the cube object, and I would like to get the ''Text'' component from the ScoreText, child of a canvas. (I watched a video and I have not really an idea about what I am doing..)
public GameObject otherGameObject;
private GUIText text;
void Awake()
{
text = otherGameObject.GetComponent<GUIText>();
}
// Update is called once per frame
void Update ()
{
if (text >= 10)
{
Debug.Log ("It works");
}
}
I am getting this error:
Hopefully someone can help me with this.
Answer by Fredex8 · Apr 03, 2016 at 02:27 PM
As the error says... you cannot use mathematical operators like greater than or equal to on text. That makes no sense.
What you need to do is check the score value which will be an int or a float, ie: a number. The text would be set to simply display this number.
This explains it all clearly enough in the first code example.
Parse it to an int first ;)
http://answers.unity3d.com/questions/37756/how-to-turn-a-string-to-an-int.html
Probably not good practice though.