Search Unity

An object reference is required to access non-static member `ScoreManager1.scoreCount'

Discussion in 'Scripting' started by Steikepanne, Nov 23, 2017.

  1. Steikepanne

    Steikepanne

    Joined:
    Jan 30, 2017
    Posts:
    34
    I think i need some help. My score and highscore is fine, but when I'm pressing the button it doesn't add score.
    In my old ScoreManager script the "score" were an int, and now its a float. I think that the problem is there, but since I'm a noob at programming I'm not sure, and don't know how to fix it.

    Problem lies in the DestroyOnClick script down below.

    ScoreManager code

    Code (CSharp):
    1. public class ScoreManager1 : MonoBehaviour {
    2.  
    3.     public Text scoreText;
    4.     public Text highScoreText;
    5.  
    6.     public float scoreCount;
    7.     public float highScoreCount;
    8.  
    9.     public float pointsPerSecond;
    10.  
    11.     public bool scoreIncreasing;
    12.  
    13.     // Use this for initialization
    14.     void Start () {
    15.        
    16.     }
    17.    
    18.     // Update is called once per frame
    19.     void Update () {
    20.         if (scoreIncreasing)
    21.         {
    22.             scoreCount += pointsPerSecond * Time.deltaTime;
    23.         }
    24.        
    25.         if (scoreCount > highScoreCount)
    26.         {
    27.             highScoreCount = scoreCount;
    28.         }
    29.  
    30.         scoreText.text = "Score: " + Mathf.Round(scoreCount);
    31.         highScoreText.text = "HighScore: " + Mathf.Round(highScoreCount);
    32.        
    33.     }
    34. }
    Here is the DestroyOnClick script

    Code (CSharp):
    1. public class DestroyOnClick : MonoBehaviour {
    2.  
    3.     private float timer = 0;
    4.     public float X = 3;
    5.     public int scoreValue = 10;
    6.  
    7.     public GameObject Button;
    8.    
    9.  
    10.     void Update()
    11.     {
    12.         timer += Time.deltaTime;
    13.         if (Input.GetMouseButtonDown(0))
    14.         {
    15.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    16.  
    17.             RaycastHit hit;
    18.             if (Physics.Raycast(ray, out hit, 500.0f) && hit.transform.gameObject != null)
    19.             {
    20.                 Destroy(hit.transform.gameObject);
    21.                 ScoreManager1.scoreCount += scoreValue;  //This is the problem
    22.             }
    23.         }
    24.  
    25.         if (timer >= X) Destroy(Button);
    26.     }
    27. }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    scoreCount either needs to be static or you need to access scoreCount through an instance of ScoreManager1
     
  3. Steikepanne

    Steikepanne

    Joined:
    Jan 30, 2017
    Posts:
    34
    public static float scoreCount; of course, just one word off. Thank you for that :)
     
  4. angel_m

    angel_m

    Joined:
    Nov 4, 2005
    Posts:
    1,160
    In a similar case I tried to use an instance of the script I wanted to access but it didn't work. I had to use an static variable, and it solved the problem.