- Home /
An object reference is required to access non-static member
hey im trying to get the variable "score" from this script :
public class Despawn_Objects : MonoBehaviour { public int score ;
void OnCollisionEnter2D(Collision2D col)
{
if(col.gameObject.tag=="Present")
{
Destroy(col.gameObject);
}
score=score+1;
}
}
so i can use it in this script:
public int points;
public TextMesh tm;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
points = (Despawn_Objects.score);
GetComponent<TextMesh> ().text = points.ToString ();
}
how ever i keep getting the error "An object reference is required to access non-static member `Despawn_Objects.score"..
any help please.. i been reading but im not sure how to reference an object or how to set an instance.. I am relativly new to c#.
EDIT: now getting this problem The value of points is not actually being set to the value of the score
EDIT: now getting this problem Object reference not set to an instance of an object
With the same script, without changing anything?
never $$anonymous$$d sorted it. however the acutal appending of textmesh.text is not being = to points....
Answer by Landern · Nov 18, 2014 at 02:02 PM
This:
(Despawn_Objects.score)
In your current context it's worthless, it doesn't do anything special. It is however trying to access the reference to Despawn_Objects(some object) and the field called score, there isn't a Despawn_Objects, but it hasn't been instantiated(the object doesn't exist in the scope you're trying to access it in).
You could create a singleton, which is the process of creating an object that exists once in the scope of your game. Read about it here, it would allow you to do what you're trying to do.
First you need to understand that if the script Despawn_Objects is on the same gameobject that the script you want to call it from... you can get a reference with GetComponent much like you're doing with TextMesh on line 15.
Despawn_Objects is not a static object and so you can't just call it willy nilly. You need to get a reference to it, with the special case of whether the object is static or the variable is a class member and is marked static apposed to an instance member which exists in the scope of the lifetime of an instantiated object.
If you know the object, Use GameObject.Find, you can find it if the gameobject is tagged using GameObject.FindWithTag.
After finding the game object you will need to get the reference to the script by using GetComponent. If Despawn_Objects only exists once in your scene, you should get a reference to it in the start method/function of the script that is going to use that reference to get values from it's properties/fields.
Answer by HarshadK · Nov 18, 2014 at 02:05 PM
The error is shown because you require an object reference to access your score variable from Despawn_Objects script since your Despawn_Objects script is not static.
You can get a reference to the script using
gameObject.GetComponent<Despawn_Objects>();
and use it like:
points = gameObject.GetComponent<Despawn_Objects>().score;
considering both these scripts are on same game object.
Also calling GetComponent in each Update is not efficient and you should cache the reference but I've avoided it to make the concept clear for you.
Answer by Immanuel-Scholz · Nov 18, 2014 at 02:08 PM
The variable "score" is an "instance variable", which means every Despawn_Object - component keeps its own score (you could have many gameObjects with Despawn_Object components. And you could even have multiple Despawn_Object components on a single gameObject).
So you have to tell the compiler from which Despawn_Object - component you want the score. That is the error.
You probably want to access the score from "The only one Despawn_Object component that is at the same gameObject as my script where I want to access it". If that is so, then you can use:
void Update() {
points = GetComponent<Despawn_Object>().score;
...
}
Another common case is, that you want the score from "The one and only one Despawn_Object in existance. There is always only one single Despawn_Object - component in my whole scene. I made sure of that."
In this case, you should do:
public int points;
public TextMesh tm;
private Despawn_Object theOneAndOnlyDespawnObject;
// Use this for initialization
void Start () {
theOneAndOnlyDespawnObject = GameObject.FindObjectOfType<Despawn_Object>();
}
// Update is called once per frame
void Update () {
points = theOneAndOnlyDespawnObject.score;
GetComponent<TextMesh> ().text = points.ToString ();
}
Answer by Josusan1 · Nov 18, 2014 at 02:19 PM
neever mind sorted it but howeven the actual text mesh. test is not being update to the value of "points"....
Your answer
Follow this Question
Related Questions
An object reference is required to access non-static member 1 Answer
Referencing non-static variable from script on child C# 1 Answer
Get variable from seperate script. 1 Answer
reference is required to access non-static member `CarController.Update()' 1 Answer
Object reference not set to an instance of an object 1 Answer