Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Question Unity Lesson 5.2 Score Not Working

Discussion in 'Getting Started' started by quiatjad000, Feb 28, 2023.

  1. quiatjad000

    quiatjad000

    Joined:
    Dec 6, 2022
    Posts:
    3
    I've been following through this Unity tutorial and cannot figure out what is wrong with it. I've been looking through forums for a while and can't find what's wrong. What's going on is: the score isn't going up when you click the objects on-screen. The bad objects don't do anything yet by the way.

    Target.cs is the first, GameManager.cs is second

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Target : MonoBehaviour
    6. {
    7.  
    8.     private Rigidbody targetRb;
    9.  
    10.     private float minSpeed = 12;
    11.     private float maxSpeed = 16;
    12.     private float maxTorque = 10;
    13.     private float xRange = 4;
    14.     private float ySpawnPos = -6;
    15.  
    16.     private GameManager gameManager;
    17.  
    18.     // Start is called before the first frame update
    19.     void Start()
    20.     {
    21.         targetRb = GetComponent<Rigidbody>();
    22.         targetRb.AddForce(RandomForce(), ForceMode.Impulse);
    23.         targetRb.AddTorque(RandomTorque(), RandomTorque(), RandomTorque(), ForceMode.Impulse);
    24.         transform.position = RandomSpawnPos();
    25.         gameManager = GameObject.Find("Game Manager").GetComponent<GameManager>();
    26.     }
    27.  
    28.     // Update is called once per frame
    29.     void Update()
    30.     {
    31.        
    32.     }
    33.  
    34. Vector3 RandomForce()
    35. {
    36.     return Vector3.up * Random.Range(minSpeed, maxSpeed);
    37. }
    38.  
    39. float RandomTorque()
    40. {
    41.     return Random.Range(-maxTorque, maxTorque);
    42. }
    43.  
    44. Vector3 RandomSpawnPos()
    45. {
    46.     return new Vector3(Random.Range(-xRange, xRange), ySpawnPos);
    47. }
    48.  
    49. private void OnMouseDown()
    50. {
    51.     Destroy(gameObject);
    52.     gameManager.UpdateScore(5);
    53. }
    54.  
    55. private void OnTriggerEnter(Collider other)
    56. {
    57.     Destroy(gameObject);
    58. }
    59.  
    60. }
    61.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5.  
    6. public class GameManager : MonoBehaviour
    7. {
    8.  
    9.     public List<GameObject> targets;
    10.  
    11.     private float spawnRate = 1.0f;
    12.  
    13.     private int score;
    14.     public TextMeshProUGUI scoreText;
    15.    
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.         StartCoroutine(SpawnTarget());
    20.         UpdateScore(0);
    21.     }
    22.  
    23.     // Update is called once per frame
    24.     void Update()
    25.     {
    26.        
    27.     }
    28.  
    29.     IEnumerator SpawnTarget()
    30.     {
    31.             while (true)
    32.         {
    33.              yield return new WaitForSeconds(spawnRate);
    34.              int index = Random.Range(0, targets.Count);
    35.              Instantiate(targets[index]);
    36.         }
    37.     }
    38.  
    39.     public void UpdateScore(int scoreToAdd)
    40.     {
    41.         score += scoreToAdd;
    42.         scoreText.text = "Score: " + score;
    43.     }
    44.  
    45. }
    46.  
     
  2. RichAllen2023

    RichAllen2023

    Joined:
    Jul 19, 2016
    Posts:
    1,026
    I'm not certain if it is that, but it could be as simple as you need a capital G on line 25.

    The code's VERY case and spelling sensitive.
     
  3. AnaWilliam850

    AnaWilliam850

    Joined:
    Dec 23, 2022
    Posts:
    36
    It looks like your code is missing a few things. Here are some suggestions that might help:

    1. Make sure that you have assigned the GameManager script to the "Game Manager" object in your scene. If you haven't done this, the line gameManager = GameObject.Find("Game Manager").GetComponent<GameManager>(); won't work.

    2. Check that your scoreText variable in the GameManager script is properly linked to the TextMeshProUGUI object that you are using to display the score. If it isn't linked, the score won't be displayed correctly.

    3. Make sure that your targets have colliders attached to them. If they don't have colliders, the OnMouseDown() and OnTriggerEnter() functions won't be called.

    4. Check that your targets have the Target script attached to them. If they don't have the script, the game won't be able to access the UpdateScore() function in the GameManager script.
    If none of these suggestions solve your problem, please provide more information about what is happening when you run your game (e.g. are there any error messages in the console?).
     
  4. quiatjad000

    quiatjad000

    Joined:
    Dec 6, 2022
    Posts:
    3
    wouldnt really make sense bc the gameManager variable is named exactly that
     
  5. RichAllen2023

    RichAllen2023

    Joined:
    Jul 19, 2016
    Posts:
    1,026
    No it's not, your script literally has a capital G :D
     
  6. quiatjad000

    quiatjad000

    Joined:
    Dec 6, 2022
    Posts:
    3
    Do you mean make a G capital or lowercase? Also, which one? Because I'm assuming you're talking about the Target.cs script, which is the only script that has any G's on line 25. And there's 5 on that line.