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 Dok101 · Feb 08, 2014 at 03:09 PM · variablestatic variablenonstatic

How to access a non-static variable from another class?

Hello I am trying to make a simple menu script that goes to the level picked and changes the material of a game object. Do this I have created five scripts. Here is the code for the first one

 #pragma strict
 var Level_Select : int;
 
 function Start () {
 
 }
 
 function Update () {
 
         if (Input.GetKey ("up"))
         {
         Level_Select--;
         Debug.Log (Level_Select);
         }
             
             if (Input.GetKey ("down"))
         {
         Level_Select++;
         Debug.Log (Level_Select);
         }
 
         if (Level_Select == 1)
         {
         renderer.material.color = Color.grey;
         }
         else
         {
         renderer.material.color = Color.black;
         }
         
 }

Then in the other scripts the code is

 #pragma strict
 
 function Start () {
 
 }
 
 function Update () {
 
         if (Input.GetKey ("up"))
         {
         Main_Menu_Script.Level_Select--;
         Debug.Log (Main_Menu_Script.Level_Select);
         }
             
             if (Input.GetKey ("down"))
         {
         Main_Menu_Script.Level_Select++;
         Debug.Log (Main_Menu_Script.Level_Select);
         }
 
         if (Main_Menu_Script.Level_Select == 2)
         {
         renderer.material.color = Color.grey;
         }
         else
         {
         renderer.material.color = Color.black;
         }
         
 }

Then the other 3 scripts are similar to the last one just with "Level_Select" being a different number. I haven't added the code to change the level yet.

When the Level_Select variable isnt static I get the error "Assets/Scripts/Main Menu/Main_Menu_Script_2.js(21,38): BCE0020: An instance of type 'Main_Menu_Script' is required to access non static member 'Level_Select'." When I change the Level_Select variable to a static variable all the errors go away. So my question is, is it possible to access a non static variable in another script and if it is possible, how. Thank You

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by getyour411 · Feb 08, 2014 at 03:10 PM

 LevelSelect ls = GameObject.Find("MainMenu").GetComponent<LevelSelect>();

That's C#, use your JS equiv

Comment
Add comment · Show 1 · 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 Dok101 · Feb 08, 2014 at 03:22 PM 0
Share

Thank you I will attempt to convert that to Javascript

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

Will static variables not work if negative? 1 Answer

How to make variable accessible by other scripts but not visible in editor? 1 Answer

Passing variables' values between two or more scripts 1 Answer

Global Variable Error 1 Answer

Can I make variables depend on eachother before running the game 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