- Home /
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
Answer by getyour411 · Feb 08, 2014 at 03:10 PM
LevelSelect ls = GameObject.Find("MainMenu").GetComponent<LevelSelect>();
That's C#, use your JS equiv
Your answer
![](https://koobas.hobune.stream/wayback/20220613132916im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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