- Home /
Best way to reference static method of another script
I'm fairly new to Unity, amateur in programming in general. I've read a few things about setting things to be static, seems like a good thing (saves space in memory).
In my game I don't have many instances of the same class, but a single instance that get manipulated, pushed around and so on.
I've tried a couple different approaches when trying to access static functions from different classes, I was wondering which of them is the best one in terms of design and programming in general.
The first way, is to set my static method to take a GameObject argument. e.g.
public class MyClass: MonoBehaviour {
public static void MyMethod(GameObject myGO){
......
}
}
and then from another script, setting a public GameObject variable, dragging my object in the inspector and calling
MyClass.MyMethod(differentGameObject);
The second way is to set
public class MyClass: MonoBehaviour {
public static void MyMethod(){
......
}
}
and then from a different class setting a variable of type MyClass
MyClass DifferentGameObject;
and just use
DifferentGameObject.MyMethod();
Which one is the best when I want to manipulate an object from a different class?
???
I'm not sure what you are asking. Any more details of what you are trying to achieve?
Static functions are accessed by their class, as in the top method. The second method should throw an error. "Static member cannot be accessed by an instance" or some such like.
Answer by fafase · Jun 25, 2014 at 10:35 AM
Another one is to use an extension method:
public static class Utility{
public static void MyMethod(this GameObject objectRef){
// Acting on objectRef which is the reference to the calling object
}
}
and you call that like this
public GameObject other;
void Start(){
other.MyMethod();
}
gives it a more OOP orientation.
I really need to get my hands on the use of "this". I'm very confused about exactly how it works..
Does any of the 2 ways I mentioned have any difference when it comes to game performance? I'm working on games for mobile devices, so smart resource management does count a lot.
From what Andres mentioned, I suppose I could use this way and put these scripts in a single empty gameobject and have a more organized game scene...
About performance , not really, what you gain using a static method is little. You drop the call for a virt method that is checking if the object exists or not. Static methods are always there so this call is not used. But this is $$anonymous$$or compared to choosing what is best for you. Static is to be used when appropriate. For instance, if you modify the object members then you want an instance method since static cannot access instance members. If you are working with values from the object but not modifying them then you can go with a static method taking const parameter. But again, this is just my rule of thumb not everyone's.
For instance think of Unity's Normalize and Distance methods from Vector3, in the first case, you change the value of the Vector3 object (which is a struct but whatever), in the second you are using values from 2 vectors and returns a float without changing the values of the two objects, they use a static method.
BUT!! you will see some engines using:
float dist = vectorInstance.Distance(otherVector);
so you see that this is no general rule.
The this keyword in the case of extension method refers to the object calling the method. Actually, it is always there for any instance method but is implicit. This is why you can use this.something inside a member method.
In the case of extension it is explicitly added since the method does not know what kind of object is meant to call it. It would take too long to enter into details, the web is full of explanation on that topic.
Answer by Andres-Fernandez · Jun 25, 2014 at 10:15 AM
The main difference is that using the first way you can call MyMetyhod using any kind of object, but the second way can only use MyMethod for objects of MyClass type.
It depends on what you want to achieve. Generic function that changes the transform of the object? Cool, first way works great. Function that makes your MyClass object shrink when pressing space bar? Ok, second way may work, but only for your MyClass objects.
Answer by coldwar98 · Jun 25, 2014 at 09:30 AM
both have their own significance but i ll prefer first one in general but if you want to manipulate more variables within Mymethod you can use second one if u properly code init.
Your answer
Follow this Question
Related Questions
Having trouble calling a method between scripts without making the method static. 1 Answer
Static function won't call another static function 1 Answer
"Failed to call static function because an object was provided" error? 1 Answer
static function generates error CS0119: 1 Answer
Static vs Non-Static Methods 1 Answer