Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
0
Question by phoda · Jun 04, 2015 at 03:16 PM · functionstaticnon-static

Static vs Non-static functions and variables

I am quite new to this programming scene and try to figure out from examples but some things bother me about static and non-static things that I can't find answer online. 1. Should I reference object in script and call public functions or just set them static and call them static way (wich is faster) 2. Why cant I comminacte normal functions and static functions and when to use static functions and variables

I tried to search thoese answers on google and either i dont understand them or can't fight good explanations. Any kind of help would be good.

Thanks.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by Enum · Jun 04, 2015 at 09:10 PM

I think you should read a guide about object oriented programming in general. To understand all those concepts of classes/instances, static/non-static, inheritence and polymorphism is really worth the effort. But I will try to give you an example (that is not related to game programming on purpose):

Let's consider a class representing an emperor penguin: (Be aware that I might violate naming conventions, as C# is a new language for me, too)

 class EmperorPenguin {
     
     static string conservationStatus = "Near Threatened";
     
     float weight = 15;
     
     static bool doesItLike(Food f) {
         return f.isFish();
     }
     
     void eat(Food f) {
         if(EmperorPenguin.doesItLike(f)) {
             weight += f.getWeight();
         }
     }
 }

The variable conservationStatus is static, because it is a property of the species. Not one emperor penguin is Near Threatened but the "community" of emperor penguins. Weight is an instance variable because it is per individual: Two penguins might not share the same weight.

For the functions it's the same: All penguins eat the same food (without respect to personal preferrences like in human beings), so the method that checks whether a penguin likes a particular piece of food can be static. Eat() should increase the weight of the specific penguin eating the food, so this method must not be static.

You can see that the non-static method can call the static one, as it can be accessed from everywhere by the class-name. But the static function may not call instance methods or variables, as it belongs to all penguins, not to a single individual.

To call those non-static methods, you need to create an instance: Taking the "construction plan" (the class) and create a concrete object from it:

 EmperorPenguin p = new EmperorPenguin();
 Food f = new Food("fish", 0.5f);
 p.eat(f);
 print(p.weight);
 //You can create a second penguin with different properties:
 EmperorPenguin p2 = new EmperorPenguin();
 Food f = new Food("fish", 0.75f);
 p2.eat(f);
 print(p2.weight);
 //Possible without any instance:
 print(EmperorPenguin.conservationStatus);
Comment
Add comment · Show 6 · 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 phoda · Jun 09, 2015 at 01:35 PM 0
Share

Thanks. It made some points of coding clear!

avatar image fafase · Jun 09, 2015 at 03:36 PM 0
Share

When it comes to method, you could actually use one or the other. But it is conventional to use instance when the object is modified, else use static. Like in Vector3 class, Distance use the parameter but does not affect them so static, Normalize modifies the instance so non-static.

avatar image phoda · Jun 09, 2015 at 08:52 PM 0
Share

So far I used quite alot of public static variables mostly for easier communication between scripts (for instance if its game wide variable or cant reference script) but for functions (public static) i find it easier to use public static bool to call functions like that.

maybe iam doing quite alot of things wrong here but my games are not much complex for now so i cant really see fps drops but i try to optimize best as i can

avatar image Enum · Jun 10, 2015 at 09:57 AM 1
Share

Static or not has no big impact on performance. It's a matter of program$$anonymous$$g reusable or extendability. Program$$anonymous$$g is not all about performance. Calling one variable every frame is not worth thinking about performance. $$anonymous$$aybe think about performance where (nested) loops are.

I renew my advice to read about (good) program$$anonymous$$g in general, as you will learn why static (and making everything public) is often not really appropriate. BUT in simple programs it is not worth bothering, like it is not worth bothering about performance when calling/creating a single variable.

avatar image Enum · Jun 10, 2015 at 07:32 PM 1
Share

I never really learned C#.. I learned Java wich is quite similar. $$anonymous$$aybe someone else has a good hint?

IFs that you write by hand, that are not in loops will not really affect performance... Simple IFs or additions will always take only a few nanoseconds or less (quite complicated what happens in a processor, but that are maximum numbers). When you ask a List with 100 entries, if it contains a specific object, this will need up to 100 IFs and 100 calls to an "equals" method. This might take, lets say, 600 nanoseconds. Such points, where you operate on large datastructures, are those points where you can slowly(!) start thinking about performance. Otherwise don't bother your $$anonymous$$d with performance. Start with it, when you really have FPS problems.

A long method in general is considered not well programmed (you will learn such things by time and will experience why it's bad). If you have a method, that you can read like text, because it calls many methods with speaking names and uses variables with speaking names, then it is nice. (Of course you will at some point always need to write the IFs in the called methods.)

For more read in tutorials, here in unity answers and maybe another general program$$anonymous$$g forum. $$anonymous$$aybe even try to answer simple questions.

Show more comments

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

ERROR - An object reference is required to access non-static member 3 Answers

PlayerPrefs in static function? 1 Answer

"transform" in a static function 1 Answer

Simple static function to display text onscreen (Accessible from anywhere) 1 Answer

An object reference is required to access non-static member 0 Answers


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