Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Game Development How to Make a Video Game Score, Enemies, and Game State Script the Score Counter

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

Changing the Text based on number of flies caught

Hi, I'm up to date with the game now and I've managed to increment the score as shown in the video.

But I then got a bit daring and decided to try and change the text based on the incremented score. It's age old problem, "1 flies" doesn't make all that much much sense does it?

Here's the script I used to try and fix it.

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class ScoreCounter : MonoBehaviour {

    public static int score;
    private Text scoreCounterText;

    //Text is a special class provided by Unit not built into c#

    void Start() {
        score = 0;
        scoreCounterText = GetComponent<Text> ();
    }


    void Update() {

        //scoreCounterText.text = score + " Flies";

        if (score == 1) {
            scoreCounterText.text = score + " Fly";
        } else {
            scoreCounterText.text = score + " Flies";
        }
    }

}

i keep getting an error though and can't play the game. Any idea why? :)

1 Answer

Jack Beauchamp
Jack Beauchamp
2,411 Points

Tried your code it works fine, do mind sharing what the error is, I am guessing it is just a simple grammar error.

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

Yes I just tested it just now. Must have been a bug creeped in to Unity that was solved by simply restarting Unity :-)