Hello everyone.
I’ve been looking at some examples of C# in unity, and in some of them I see people refferencing to other scripts like this:
public Static ScriptName Instance;
public string randomString = "potatoes";
void Awake () {
Instance = this;
}
And then, on another script
someOtherRandomString = ScriptName.Instance.randomString;
Then the variable someOtherRandomString’s value would be “potatoes” as well, so we are refferencing to another script, by creating a variable which holds an actual instance to the first script.
I find this way to be way more clean and easy than using the refferencing through the inspector (because we don’t always want the user to be able to configure those refferences from the inspector view), but I wanted to ask: Is this more CPU consuming than the other methods described in the documentation? Is there any way to do this in javaScript (I’m assuming there’s one, just don’t know it). And lastly I wanted to ask: why isn’t this method described in the unity documentation (correct me if i’m wrong but i didn’t see it the last time I checked)
And also, I would like to ask: If I use this method, I’m assuming there’s only one Instance of that script’s class running at the same time. Otherwise the variable Instance would hold two instances of the class at the same time, which can’t be as far as I know. Am I right?
Thank you