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 bman85 · Nov 15, 2013 at 01:19 AM · c#variablestaticbooleans

Static variable error

I have 2 c# scripts and I am trying to access a boolean variable from one of them in the other one. Everything seems to be public, so it seems like I should be able to access the variable. I receive an error though that says "An object reference is requred to access non-static member FinishLevel.Level1Complete". Here are the scripts:

 using UnityEngine;
 using System.Collections;
 
 
 public class FinishLevel : MonoBehaviour {
     
     public bool Level1Lost;
     public bool Level1Complete;
     // Use this for initialization
     public void Update () {
         if(Level1Complete){
             Application.LoadLevel("Win 1");
         }        
         if(Level1Lost){
             Application.LoadLevel ("LostLevel1");
         }
     }
     
     // Update is called once per frame
     public void OnTriggerEnter(Collider other){
         if(GameObject.FindWithTag ("Finish")){
             Level1Complete = true;
         }
         if(GameObject.FindWithTag ("DeathZone")){
             Level1Lost = true;
         }
     }
 }

And I am trying to access Level1Complete variable using this one:

 using UnityEngine;
 using System.Collections;
 
 public class LevelSelectGUI : MonoBehaviour {
 
     public GUIStyle l1Style;
 
     void OnGUI () {
         if(GUI.Button(new Rect(Screen.width/2+180,Screen.height/2-60,40,40),"1", l1Style)){
             if(FinishLevel.Level1Complete){
                 Application.LoadLevel ("Level 1");
             }
     
         }
     }
 }
 
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by rutter · Nov 15, 2013 at 01:25 AM

Instance variable:

 public bool Level1Lost;

Static variable:

 public static bool Level1Lost;

The key difference is whether the variable is associated with an object (each FinishLevel object has its own copy of the variable) or with the class (there is only one variable, which is shared by every object that can access it).

If you're not clear on the difference, Google around for a few articles or blog posts and find one that makes sense to you.

One note specific to Unity: remember that a static variable will not be automatically reset when you load a new scene. You can reset it yourself easily enough, as long as you're careful.

Having loads of public static variables tends to make code hard to debug and maintain. It is sometimes an acceptable band-aid for simple flags, counters, and the like.

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 aldonaletto · Nov 15, 2013 at 02:16 AM

As @rutter said, Level1Lost is a instance variable: it exists in every instance of FinishLevel. Since many objects may have a FinishLevel instance attached, you must define which object you're interesting in, then get a reference to its FinishLevel script in order to access its variable Level1Lost. Supposing that the object you want is called "LevelControl", you could get the instance reference like this:

 using UnityEngine;
 using System.Collections;
  
 public class LevelSelectGUI : MonoBehaviour {
      
     public GUIStyle l1Style;
     private FinishLevel finishLevelScript;
 
     void Start(){
         // get a reference to the object you want:
         GameObject levelCtrl = GameObject.Find("LevelControl");
         // get a reference to its FinishLevel script:
         finishLevelScript = levelCtrl.GetComponent<FinishLevel>();
     }
 
     void OnGUI () {
         if (GUI.Button(new Rect(Screen.width/2+180,Screen.height/2-60,40,40),"1", l1Style)){
             // access the variable via script reference:
             if (finishLevelScript.Level1Complete){
                 Application.LoadLevel ("Level 1");
             }
         }
     }
 }

But if you only has a single instance of FinishLevel in your whole game, you may declare Level1Complete as static and access it like you tried to do:

      if (FinishLevel.Level1Complete){

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

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

can i change a variable from non-static to static at runtime in c#? 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Where is the Mistake in my Coin System? 1 Answer

I´ve got a problem with my Coin System. It always shows 1 Coin. 2 Answers


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