Search Unity

Question An object reference is required for the non-static field, method, or property

Discussion in 'Scripting' started by stevensia95, Feb 21, 2024.

  1. stevensia95

    stevensia95

    Joined:
    Dec 21, 2023
    Posts:
    1
    Hello. I'm building a simple 2D roguelike game. I am trying to make the skill UI will show out when the score reach the score set. but when i insert the code it show “An object reference is required for the non-static field, method, or property” this error.

    this the coding for scoreManager
    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using TMPro;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6.  
    7. public class scoreManager : MonoBehaviour
    8. {
    9.     public TextMeshProUGUI scoreText;
    10.     public static event Action OnScoreReached;
    11.  
    12.     private int m_Score;
    13.  
    14.     public int Score
    15.     {
    16.         get => m_Score;
    17.         set
    18.         {
    19.             m_Score = value;
    20.             if (m_Score % 10 == 0)
    21.                 OnScoreReached?.Invoke();
    22.         }
    23.  
    24.     }
    25.  
    26. }
    27.  
    28.  
    show score script (line 8)
    Code (CSharp):
    1. public class scoreDisplay : MonoBehaviour
    2. {
    3.     TextMeshProUGUI scoreText;
    4.  
    5.     void Update()
    6.     {
    7.         // Display the current score
    8.        scoreText.text = "Score : " + scoreManager.Score; //error
    9.  
    10.     }
    11. }
    12.  
    calculate the score (line 8)
    Code (CSharp):
    1. private void Awake()
    2.     {
    3.         var enemy = LocateEnemy();
    4.         if (enemy != null)
    5.         {
    6.             _direction = MoveDirection(enemy.transform);
    7.             transform.rotation = Quaternion.LookRotation(Vector3.forward, _direction);
    8.             scoreManager.Score += 1;  //error line
    9.         }
    10.         else
    11.         {
    12.             // Handle the case when there is no enemy.
    13.         }
    14.     }
     
  2. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,175
    You're trying to use the
    scoreText
    field without having first initialized or set it to a value. If it's on the object with the
    TextMeshProUGUI
    component you can use
    GetComponent
    .

    Code (csharp):
    1. public class scoreDisplay : MonoBehaviour
    2. {
    3.     TextMeshProUGUI scoreText;
    4.  
    5.     void Start()
    6.     {
    7.         scoreText = GetComponent<TextMeshProUGUI>();
    8.     }
    9.  
    10.     void Update()
    11.     {
    12.         if (scoreText != null)
    13.         {
    14.             // Display the current score
    15.             scoreText.text = "Score : " + scoreManager.Instance.Score;
    16.         }
    17.         else
    18.         {
    19.             Debug.LogError($"scoreText hasn't been set.", this);
    20.         }
    21.     }
    22. }
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    While @Ryiah is correct that you'll probably get an error there, the particular error you have at this line isn't a reference to scoreText. It's actually due to scoreManager.Score. Since scoreManager is not a static class, you can't just call the Score property on it directly. You'll need to either have a variable that you assign scoreManager to. Or, as @Ryiah did fix in the response, make a static variable called Instance in scoreManager that is of type scoreManager and assign itself in Awake, thus creating a singleton.

    Code (CSharp):
    1. scoreText.text = "Score : " + scoreManager.Score; //error