Hi, I’m Ryan and I would like to make a central script with all my variables stored in it. The problem is how do I make such a script?
I tried extend, but then I had to attach all variables to every other script. Does anybody knows how to make my central script work?
Thanks a lot…
Create a static instance of your central script
public class MyMainScript : MonoBehaviour
{
//Declare a static instance of this script
public static MyMainScript instance;
//Decalre all vaiables as public variables
public int x=0;
public float y=1.0f;
//Asiign the static instance on Awake()
void Awake()
{
instance = this;
}
}
Access any variables of your central script from any other script using
int a = MyMainScript.instance.x;
float b = MyMainScript.instance.y;