Hey guys,
So I am trying to get my brother familiar with programming, since that is what he says he wants to do as a career. Therefore, I have set-up this simple recreation of PacMan to demonstrate moving a character, dynamically interacting with objects, and eventually adding enemy AI. Then I ran across a problem I have encountered twice before, but I forgot how I fixed - my Score and Pellets Collected GUI.Labels were updating, but leaving previous font behind. I’ve attached a screenshot showing what I mean - notice how Score and Pellets Collected have other numbers remaining behind them, while Pellets Remaining does not.
I currently have my GUI in my GameManager class, but it will likely be moved by the end of the project, if necessary. I use GameManager as a warehouse for scores, stats, and other variables of the sort.
public class GameManager : MonoBehaviour {
public int score;
public int pelletsGot;
public int pelletsLeft;
public int pelletScoreValue = 10;
public float timer;
private float startTime;
// Use this for initialization
void Start () {
startTime = Time.time;
}
// Update is called once per frame
void Update () {
timer = Time.time - startTime;
pelletsLeft = GameObject.FindGameObjectsWithTag("Collectible").Length; // Counts the number of pellets
}
void OnGUI(){
GUI.Label(new Rect(10, 10, 100, 50), "Score: " + score.ToString());
GUI.Label(new Rect(10, 65, 100, 50), "Pellets Collected: " + pelletsGot.ToString());
GUI.Label(new Rect(10, 120, 100, 50), "Pellets Remaining: " + pelletsLeft.ToString());
}
public void CollectPellet(){
score += pelletScoreValue;
pelletsGot++;
}
}
The problem is on the Score and Pellets Collected GUI.Labels, and not the Pellets Left GUI.Label. I have a feeling this is because pelletsLeft is being updated in the Update method, while Score and Pellets Collected are only being updated in the OnGUI method.
If I am correct, how would I work around this? I am a new game developer myself, but I wanted to show my brother what was possible with less than a year of experience. I don’t know all the tricks yet, so any tips are welcome.
And yes, I have checked to see if this has been asked before, which I found nothing of use. I have spent the last 2 hours pondering over this. Hopefully some one in the community has the answer.
Thanks in advance.
[2316-screen+shot+2012-07-30+at+9.01.03+pm.png|2316]