Tutorial tells you to add line of code that doesn't work.

On Lesson 5.2 - Keeping Score - Unity Learn task 5 it tells you to add code to get the game to start recording a score. The code is this.

public class GameManager : MonoBehaviour
{
    public List<GameObject> targets;
    private float spawnRate = 1f;
    public TextMeshProUGUI scoreText;
    private int score;

    // Start is called before the first frame update
    void Start()
    {
        StartCoroutine(SpawnTarget());
        score = 0;
        UpdateScore(0);
    }

    // Update is called once per frame
    void Update()
    {
       
    }


    IEnumerator SpawnTarget()
    {
        while (true)
        {
            yield return new WaitForSeconds(spawnRate);
            int index = Random.Range(0, targets.Count);
            Instantiate(targets[index]);

            UpdateScore(5);
        }


        void UpdateScore(int scoreToAdd)
        {
            score += scoreToAdd;
            scoreText.text = "Score:" + score;
        }
    }
}

The added lines are 18, 36 and the void update score method. As the script is shown on here it won’t work. It shows the error message in the script,
Severity Code Description Project File Line Suppression State
Error CS0103 The name ‘UpdateScore’ does not exist in the current context Assembly-CSharp C:\Users\User\Desktop\Create with code\Prototype5\Assets\Scripts\GameManager.cs 18 Active
But if I completely remove line 18 it works as it should. So why does the tutorial tell you to add line 18 in the start? and how come it works in the tutorial?

You have placed your UpdateScore method inside the SpawnTarget() function

See screenshot from tutorial and compare with your script:

1 Like

Yes and so the the tutorial shows you to do that. I have worked out that it was the place of } that was the problem or reason it didn’t work. Is there any benefit to having it so the extra line of code fits in? As it works same with or without it. The line I’m talking about is line 13 UpdateScore(0); in start.

I watched it, and it does not. They place the UpdateScore method outside of the SpawnTarget method.

1 Like

Well I’m looking at something totally wrong then as I’m thinking lines 27 to 37 in the screenshot you posted are inside the spawn target function and the Update score is line 35. Like in my script lines 23 to 32 in the spawn target function and the update score is line 31. So can you please explain how it is in side the spawn target method in one and outside it in the other?

I’m probably talking daft but it could be that you spawn the target, shoot it, and then once you’ve shot it, that adds to the score?

In @Valjuin 's screenshot, the SpawnTarget method starts at line 27 & ends at line 37, while the UpdateScore method starts at line 39 & ends at line 43. The first set of opening/closing curly-braces determine where a method starts and ends.

In this example, the UpdateScore method is outside of the SpawnTarget method, which is what the tutorial shows:

IEnumerator SpawnTarget()
//Start of SpawnTarget
{
   while(true)
   {
      yield return new WaitForSeconds(spawnRate);
      int index = Random.Range(0, targets.Count);
      Instantiate(targets[index]);

      UpdateScore(5);
   }
}
//End of SpawnTarget

void UpdateScore(int scoreToAdd)
//Start of UpdateScore
{
   score += scoreToAdd;
   scoreText.text = "Score: " + score;
}
//End of UpdateScore

In your original code snippet, you’ve placed the UpdateScore method inside the SpawnTarget method:

IEnumerator SpawnTarget()
//Start of SpawnTarget
{
   while(true)
   {
      yield return new WaitForSeconds(spawnRate);
      int index = Random.Range(0, targets.Count);
      Instantiate(targets[index]);

      UpdateScore(5);
   }

   void UpdateScore(int scoreToAdd)
   //Start of UpdateScore
   {
      score += scoreToAdd;
      scoreText.text = "Score: " + score;
   }
   //End of UpdateScore
}
//End of SpawnTarget
2 Likes