Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

An object reference is required to acces non-static member "UnityEngine.GUITEXT.text"

Discussion in 'Scripting' started by Polymath, Jul 2, 2015.

  1. Polymath

    Polymath

    Joined:
    Jun 28, 2015
    Posts:
    5
    Hello, I'm trying to modify a text with a variable.
    To do so I've created this Script and I call the function AddPoint from another script when something happens.
    This script is attached as a component to a gui text that I created from Game Object --> UI --> Text , and I called it guiScore.
    The problem is that I get the error in the Title:
    An object reference is required to acces non-static member "UnityEngine.GUITEXT.text"

    And I don't know how to fix it.
    Here's my code: (I'm using Unity5 and C#)
    Code (CSharp):
    1. public class Score : MonoBehaviour {
    2.  
    3.     static int score = 0;
    4.     static int highscore = 0;
    5.    
    6.     static public void AddPoint(){
    7.         score++;
    8.         if (score > highscore) {
    9.             highscore = score;
    10.         }
    11.     }
    12.  
    13.     void Update () {
    14.         GUIText.text = "Score: " + score + "\n Highscore: " + highscore;
    15.     }
    16. }
    17.  
    To me it looks like as if I have to add something to the Gui Text box, but I don't know what.

    Thank you very much!
     
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    899
    Try this:
    Code (CSharp):
    1. public class Score : MonoBehaviour {
    2.     static int score = 0;
    3.     static int highscore = 0;
    4.  
    5.     Text myText;
    6.  
    7.     void Start() {
    8.         myText = GetComponent<Text>();
    9.     }
    10.  
    11.     static public void AddPoint(){
    12.         score++;
    13.         if (score > highscore) {
    14.             highscore = score;
    15.         }
    16.     }
    17.     void Update () {
    18.         myText.text = "Score: " + score + "\n Highscore: " + highscore;
    19.     }
    20. }
     
  3. Polymath

    Polymath

    Joined:
    Jun 28, 2015
    Posts:
    5
    Hey, thank you for your answer, but I get an error. Line 5 in your code: The type or namespace name "Text" could not be found.

    EDIT:

    I maneged to fix that adding this line on the top:
    Code (CSharp):
    1. using UnityEngine.UI;
    But now I get the following error:
    Object reference not set to an instance of an object
    Score.Update.
    The error is in line 24 from the code on top:
    Code (CSharp):
    1. myText.text = "Score: " + score + "\n Highscore: " + highscore;
     
    Last edited: Jul 2, 2015
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    you need to drag the relevant text field into the inspector slot to create the reference to it
     
  5. Polymath

    Polymath

    Joined:
    Jun 28, 2015
    Posts:
    5
    Well, for some reason now I can't even drag something:

    Anyway, what do you mean by: Drag the relevant text field?
    What is exactly the relevant text field?
    The code (I think that I haven't changed anything but for some reason, as I said, now I can't even dragg something to the inspector) is the following:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.  
    5. public class Score : MonoBehaviour {
    6.  
    7.     static int score = 0;
    8.     static int highscore = 0;
    9.  
    10.     static public void AddPoint(){
    11.         score++;
    12.         if (score > highscore) {
    13.             highscore = score;
    14.         }
    15.     }
    16.  
    17.     void Update () {
    18.         GUIText.text = "Score: " + score + "\n Highscore: " + highscore;
    19.     }
    20. }
     
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    the relevant text field would the one you are trying to update

    use @PGJ code but put "public" at the front of line 5, if you don't include that c# defaults to a private variable (i.e. not accessible in the inspector, shouldn't show at all but I guess you found the setting for debug mode somehow)