Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by GKdev1980 · Jun 25, 2014 at 09:10 AM · static-function

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kiwasi · Jun 25, 2014 at 09:55 AM 0
Share

???

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.

3 Replies

· Add your reply
  • Sort: 
avatar image
3

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image GKdev1980 · Jun 25, 2014 at 10:40 AM 0
Share

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...

avatar image fafase · Jun 25, 2014 at 10:57 AM 0
Share

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.

avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
-1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges