As i saw as reponse for a question somewhere here, i use this in order to have variables used in all the project.
public static class GlobalVariables{
public static string myVariable
}
And i use it from anywhere in my project in this way:
GlobalVariables.myVariable = "something";
My question is: can i use in some way like this for call functions from any script?
Thanks in advance.
Hello there,
Just like a static variable can be accessed from another script, so can a function. It’s very common for Util classes to have lots of static functions that can be called from anywhere.
Here is an example from my Utils class:
public class Utils : MonoBehaviour
{
public static int GenerateRandXDigitCode(int nbOfDigits)
{
string toReturn = "";
for (int i = 0; i < nbOfDigits; ++i)
{
int rand = UnityEngine.Random.Range(1, 10);
toReturn += rand.ToString();
}
return int.Parse(toReturn);
}
}
You can call this function from pretty much anywhere (assuming there are no namespaces involved) with the following line:
int myInt = Utils.GenerateRandXDigitCode(4);
Note: My class derives from Monobehaviour because of some other code I do in there, but yours doesn’t necessarily need to.
I hope that helps!
Cheers,
~LegendBacon