Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Score Counter Method Not Working?

Discussion in 'Getting Started' started by ThrowbackZac, Apr 21, 2021.

  1. ThrowbackZac

    ThrowbackZac

    Joined:
    Aug 9, 2019
    Posts:
    4
    So I'm fresh to unity and programming in general (under a week), I was following a Udemy course but the instructor told us to figure out a score counter on our own. I feel my logic is sound but I guess it clearly isn't.

    I have 3 scripts I feel are important to this problem a Score, Coin, and GameManager.

    Score:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4. public class Score : MonoBehaviour
    5. {
    6.     public Text scoreText;
    7.     public int scoreInfo = 0;
    8.  
    9.     public void ScorePlus()
    10.     {
    11.       scoreText.text = "Score: " + scoreInfo.ToString();
    12.       scoreInfo++;
    13.     }
    14. }
    15.  
    Coin:
    Code (CSharp):
    1. using UnityEngine;
    2. public class Coin : MonoBehaviour
    3. {
    4.     Score score;
    5.    
    6.    
    7.     private void Awake()
    8.     {
    9.         score = GetComponent<Score>();
    10.     }
    11.  
    12.  
    13.     public void OnTriggerEnter(Collider other)
    14.     {
    15.  
    16.         if (other.gameObject.tag == "Player")
    17.         {
    18.            
    19.             Destroy(gameObject);
    20.             score.ScorePlus();
    21.         }
    22.     }
    23. }
    (Line 20 is giving me error Object reference not set to an instance of an object)
    GameManager:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. public class GameManager : MonoBehaviour
    4. {
    5.     [SerializeField] private GameObject mainMenu;
    6.     [SerializeField] private GameObject inGameUI;
    7.     [SerializeField] private GameObject gameOverScreen;
    8.     public static GameManager instance = null;
    9.     private bool playerActive = false;
    10.     private bool gameOver = false;
    11.     private bool gameStarted = false;
    12.     public bool PlayerActive { get { return playerActive; } }
    13.     public bool GameOver { get { return gameOver; } }
    14.     public bool GameStarted { get { return gameStarted; } }
    15.     private void Awake()
    16.     {  
    17.         inGameUI.SetActive(false);
    18.         gameOverScreen.SetActive(false);
    19.         if (instance == null)
    20.         {
    21.             instance = this;
    22.         } else if (instance != this)
    23.         {
    24.             Destroy(gameObject);
    25.         }
    26.         DontDestroyOnLoad(gameObject);
    27.     }
    28.     public void PlayerCollided()
    29.     {
    30.         inGameUI.SetActive(false);
    31.         gameOverScreen.SetActive(true);
    32.         gameOver = true;
    33.     }
    34.     public void PlayerStartedGame() {
    35.         playerActive = true;
    36.     }
    37.  
    38.     public void EnterGame()
    39.     {
    40.         mainMenu.SetActive(false);
    41.         inGameUI.SetActive(true);
    42.         gameStarted = true;
    43.     }
    44. }
    I have my score script attach and only attached to my scoreText as shown here:
    upload_2021-4-21_0-37-37.png
    Basically I'm trying to only have the score show up on the screen in the inGameUI gameState rather than from the beginning like most online tutorials are showing and I think somehow I'm running into a problem there. Thanks in advance for explanations and help.
     
  2. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,510
    Your Coin script is expecting the Score script to be attached to the same object. GetComponent will only look for scripts of that type on the same object that that script is running on.

    In all likelihood, what you probably want to be doing instead is keeping track of the player's score in the GameManager. You can then increment the score from within the Coin script by calling GameManager.instance.IncreaseScore() (note: create this method first).

    It looks like you're already doing some UI management inside your GameManager, so create a reference there to your score UI element and update the text anytime that IncreaseScore method is called. There are better ways to do this, but this should get you moving again for now.
     
  3. ThrowbackZac

    ThrowbackZac

    Joined:
    Aug 9, 2019
    Posts:
    4
    It works thanks for the help, I feel like a dummy for not knowing about GetComponent. Looks like I got a lot of catching up to do with the documentation. Thanks again!
     
    Schneider21 likes this.
  4. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,510
    No worries! We're all constantly learning here. Keep at it!
     
    ThrowbackZac likes this.