public int final_level_id;
public static int current_score;
public static int high_score;
public static int current_level = 0;
public static int unlucked_level;
// Use this for initialization
void Start () {
/*
* Built-in function.
* This will make sure that this game object does not
* get destroyed when a new scene is loaded.
*/
DontDestroyOnLoad(gameObject);
}
// Update is called once per frame
void Update () {
print ("Level: " + (current_level+1) + "Final level: " + (final_level_id+1));
}
//You cannot call non-static variables/functions in a static variable/function.
public static void level_completed()
{
//If the current level is less than the final level, load the next level.
if(current_level < final_level_id)
{
//Increases the current level.
current_level++;
/*
* Built-in class and function.
* Loads the level(scene) with the specified id.
* All levels(scenes) must be added to the build setting in Unity.
* When a new level is loaded, all the code is refreshed(restarted).
* We need to save the current_level variable, however.
*/
Application.LoadLevel(current_level);
}
//Else, game over.
else
{
print ("Congratulations, you win!");
}
}
}
Inside this class, I make a public float called ‘finallevelid’, then try to use it inside the static function ‘level_completed’. However, I get an error thrown saying:
Assets/Scripts/GameManager.cs(39,36): error CS0120: An object reference is required to access non-static member `GameManager.final_level_id’
I have tried making an object reference, but I think I am doing it wrong.
I would like to thank anyone for their help in advance.
In order to access variables that are not static from a static function, you need to have a reference to an instance of the class the variable belongs to. If your GameManager class is attached to an object in the scene, then this instance already exists, but currently you don’t have a way to access it. The situation you are in calls for the use of the Singleton design pattern.
Basically, a Singleton is a class that will only ever have one instance during a scene; this instance is stored in a static variable so that it can be accessed by any other script. Singletons are useful for managers in Unity, because you can have non-static variables that are exposed in the Editor, yet can still be accessed by other scripts. Example:
public class GameManager : MonoBehaviour {
public static GameManager Instance;
void Awake(){
Instance = this;
}
}
When Awake is called on your instance of the GameManager script attached to an object in the scene, it will set itself to the static variable Instance so that any script can access the non-static variables it possesses. In your case, you can access the final_level_id variable by calling GameManager.Instance.final_level_id.
To make this work, either ‘finallevelid’ needs to be static or you need to make ‘level_completed’ not be static. You cannot have a static function accessing non-static data in the class this way. A function that is not static can access both static and not static variables, but then other classes that want to use that function would need a reference to the component. They could not call it using the class name.