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

The Name "scoreCounter" does not exist in the current context.

Even though scoreCounter is made public the notification continues to say it does not exist. Here are my two scripts for Fly pick up and Score Counter I probably have something misspelled. Thanks for the help. FlyPick up.cs using UnityEngine; using System.Collections;

public class FlyPickup : MonoBehaviour { [SerializeField] private GameObject pickupPrefab;

void OnTriggerEnter(Collider other) {

if (other.CompareTag ("Player")) {

        Instantiate(pickupPrefab, transform.position, Quaternion.identity);

        FlySpawner.totalFlies--;

        //update the score
        ScoreCounter.score++;

        Destroy (gameObject);

    }
}

}

ScoreCounter.cs using UnityEngine; using System.Collections; using UnityEngine.UI;

public class ScoreCounter : MonoBehaviour {

public static int score;
private Text scoreCounterText;


// Use this for initialization
void Start () {
    score = 0;
    scoreCounterText = GetComponent<Text> ();
}

// Update is called once per frame
void Update () {
    scoreCounterText.text = scoe + " Flies";
}

}

Can someone please help!

4 Answers

The only thing that looks wrong, to me, is the typo in your ScoreCounter.cs script - "scoe" - but that would have probably been the name reported in console

Benjamin Gooch
Benjamin Gooch
20,367 Points

I'm having the same problem and I quadruple checked for typos.

If it helps at all, I'm using VIsual Studio to do my scripting. I haven't had any problems thus far, and even had no issues when using the FlySpawner reference earlier in this same script.

Anyone have an idea what might be going on here?

Benjamin Gooch
Benjamin Gooch
20,367 Points

So, in Visual Studio, it reports this as an error, but Unity still runs just fine. It doesn't flag it as an issue when loading in the script, and the game works exactly as expected. Weird.

Glen Hayes
Glen Hayes
5,798 Points

Same problem here with Visual Studio but unity wont let me compile and enter game mode because of the error??

Henrique Prates
Henrique Prates
4,726 Points

I was having the error, what I think that fixed it was saving both script tabs that we edit in this video; it's possible that some people would forget to save both, since he only reminds to save it after working on one.