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

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#)

public class Score : MonoBehaviour {

    static int score = 0;
    static int highscore = 0;
   
    static public void AddPoint(){
        score++;
        if (score > highscore) {
            highscore = score;
        }
    }

    void Update () {
        GUIText.text = "Score: " + score + "\n Highscore: " + highscore;
    }
}

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!

Try this:

public class Score : MonoBehaviour {
    static int score = 0;
    static int highscore = 0;

    Text myText;
  
    void Start() {
        myText = GetComponent<Text>();
    }

    static public void AddPoint(){
        score++;
        if (score > highscore) {
            highscore = score;
        }
    }
    void Update () {
        myText.text = "Score: " + score + "\n Highscore: " + highscore;
    }
}

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:

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:

myText.text = "Score: " + score + "\n Highscore: " + highscore;

you need to drag the relevant text field into the inspector slot to create the reference to it

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:

using UnityEngine;
using System.Collections;


public class Score : MonoBehaviour {
  
    static int score = 0;
    static int highscore = 0;
  
    static public void AddPoint(){
        score++;
        if (score > highscore) {
            highscore = score;
        }
    }
  
    void Update () {
        GUIText.text = "Score: " + score + "\n Highscore: " + highscore;
    }
}

the relevant text field would the one you are trying to update

use @PGJ_1 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)