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