Hi there,
I am trying to create and acess a static function from separate scripts.
smokeCloudScript.
///this function is called from the script below.
static function loadCloud()
{
restartCloud();///of note I can not call this function as it is not static
}
/// in another script i call the above like this.
///reason being to save a GetComponent on a instance, call that that is used 100s of times per game.
///smoke cloud is the instance
///smokeCloudScript is the attached holder of the static function
///loadCloud is the static function, defined above
smokeCloud.smokeCloudScript.loadCloud();
Doing it like won’t make much sense since you are making the function static and calling another function. If that function is static then why not just call that? Another way you can call static functions without making all the values is to do this…
public static Scriptname instance;
void Awake()
{
instance = this;
}
public void Something()
{
// do something
}
Then in another script you can call your public function like this…
void Start()
{
Scriptname.instance.Something();
}
Does that help?
Ok, anyway you could translate that to JS?
Declaring a method as static means that the method cannot be called through an instantiated object. Static methods can only be called through the class name.
class ClassA {
public static void HelloWorld() {
Debug.Log("Hello World");
}
}
class ClassB {
ClassA instanceOfClassA = new ClassA();
public void HelloWorld() {
instanceOfClassA.HelloWorld(); // This is wrong. This will fail to compile because ClassA.HelloWorld() is declared static.
ClassA.HelloWorld(); // This is correct. This is how you properly call a static method in another class.
}
}