Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Josusan1 · Nov 18, 2014 at 01:58 PM · referencenon-static

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image HarshadK · Nov 18, 2014 at 02:15 PM 0
Share

EDIT: now getting this problem Object reference not set to an instance of an object

With the same script, without changing anything?

avatar image Josusan1 · Nov 18, 2014 at 02:19 PM 0
Share

never $$anonymous$$d sorted it. however the acutal appending of textmesh.text is not being = to points....

4 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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 ();
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges