- Home /
Understanding static methods and object referencing.
Pretty new to c#, but I'm trying to store all of my variables that I use within a level, in one script. The script look like this:
using UnityEngine;
using System.Collections;
public class LevelScript : MonoBehaviour {
public int loadingCount;
public int loadingCountGround;
public int boardChunkCount;
public int sceneBoundHeight;
public int sceneBoundWidth;
public Vector3[] GroundWallPosition;
}
To my understanding, I have not declared anything as static. However, I get the 'An object reference is required for the non-static etc.' from other scripts that modify the variables. Such as:
LevelScript.loadingCountGround ++;
Could anyone break down the use for static variables, why would I need them, and is there a good solution to modify and call upon other variables from other scripts that doesn't use static variables?
Ok so after taking advice, the main problem now resides in my Vector3[]. When I make it static, then my line of code:
foreach (Vector3 wallPosition in LevelScriptCheck.GroundWallPosition)
Throws me an error..."Static member `LevelScript.GroundWallPosition' cannot be accessed with an instance reference, qualify it with a type name ins$$anonymous$$d"
Answer by $$anonymous$$ · Aug 20, 2014 at 06:19 PM
static variables are used for non object specific things, like in a game where every gun uses the same ammo... there would be a static int called ammo, this means no matter what object is active when the ammo is decreased or increased it does it for every weapon. what the error means is that you have not selected what script you want to change IE if every weapon had different ammo, id need to select what weapon i want to change the values of,
in your case you want to increment LevelScript.loadingCountGround but to do this your need a reference to that scrip Like so,
public LevelScript TheScript;
void Thefunction
{
TheScript.loadingCountGround++
}
sorry if this doesnt make much sense, please message back if it doesnt and i will try and explain better :D
I tried this, and after replacing many lines of code in 10+ scripts, unfortunately it gives me the same error... except en mass.
Answer by smoggach · Aug 20, 2014 at 06:21 PM
LevelScript.loadingCountGround
This line is assuming that loadingCountGround is a static public variable.
LevelScript is the class name. You need to modify an INSTANCE of LevelScript. To get that instance you can use GetComponent();
So in conclusion to access the variables in your code you need to use GetComponent().loadingCountGround++;
GetComponent(???).loadingCountGround++ Sorry you'll have to put this in laymans terms. What exactly is the context here? And how would I modify an instance of a script? There's only one version at any given time, can't the engine just identify it?
Answer by Pecek · Aug 20, 2014 at 06:24 PM
Static means it will be the same value on every instance, that's why you don't have to specify it, it's good when you want only one instance(usually audio/network/etc manager).
Public means you can modify that value outside of the script(common usage for public values are health modification - a bullet can modify the enemy's health).
In your case you can declare your variables as "public static" and the
LevelScript.loadingCountGround ++;
will work, or you have to specify the instance of the class either by declaring it as a public value and drag it in the inspector or by searching trough the gameobjects to find an instance of it. In this case static variables are a better option in my opinion since you probably don't want to have multiple instances, this way you can keep your hair when a few weeks later everything stops working because you accidentaly draged an another instance to the scene.
Btw search for the singleton pattern, you probably will find a much more clear answer to this problem.