- Home /
Question by
vincentamato · Dec 12, 2014 at 03:06 AM ·
guigamescoreguitext
Score going up every second
Hi. I want the player's score in my game to go up every second. I have tried this but I get an error saying, "Cannot implicitly convert type string' to
int'" Here is my code:
using UnityEngine;
using System.Collections;
public class AddToScore : MonoBehaviour {
private int now = 3;
private int score = "0";
public GUIText scoreLabel;
void Start(){
scoreLabel.text = score;
}
void Update(){
InvokeRepeating ("AdToScore", 1, 1);
}
void AdToScore(){
if (now > 0) {
score = score + 1;
}
}
}
Please help. Thanks!
Comment
Best Answer
Answer by Trungdv · Dec 12, 2014 at 03:24 AM
You have 3 mistakes in that code.
First:
private int score = "0";
Make a integer get a string value. It should be:
private int score = 0;
Second:
scoreLabel.text = score;
Make a text property (which only accept string) get a integer value. It should be:
scoreLabel.text = score.ToString();
Third:
After increase your score variable, you need update the label, if you do not update, you can not see the change on the label.
void AdToScore(){
if (now > 0) {
score = score + 1;
scoreLabel.text = score.ToString();
}
}