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
19
Question by yosh · Dec 23, 2009 at 09:35 AM · scriptingbasicsstaticstatic-varsstatic-function

When should i use static functions?

Hi, can anyone explain it to me why and when should i use static-functions? Thx

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

2 Replies

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

Answer by runevision · Dec 23, 2009 at 10:21 AM

Static functions enable you to call code without an instance of the class in which it's defined.

You cannot use instance data in a static function.

Normally, a static function should be self-contained. I.e. it should perform its function without requiring or saving any stateful data. A classic static function would be something like this (C#):

// MyClass.cs (C#): public class MyClass { public static float Multiply (float a, float b) { return a*b; } }

// MyClass.js (Javascript): static function Multiply (float a, float b) : float { return a*b; }

You can then call this function directly on MyClass without requiring an instance object of MyClass:

product = MyClass.Multiply(3, 2);

There are some different use cases though, where static functions do alter state that is saved in static variables. One example of this is the singleton programming pattern.

There are no "rules" for when to use static functions. You simply need to understand what it is and how it works, and determine for yourself in each situation if a static function is a good solution for the problem at hand.

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 ina · Feb 19, 2012 at 01:23 AM 0
Share

I'm still not certain what constitutes stateful and stateless data. Is the transform of an object considered stateful?

avatar image cregox · Apr 10, 2012 at 08:37 PM 0
Share

@ina we usually say raw HTTP, for instance, is stateless because each HTTP request keeps no information about the last request. Stateful is the opposite. It basically means: one which is stateful is able to keep data stored to be used out of its own scope, and/or able to retrieve it.

avatar image
0

Answer by rakeshparihar · May 17, 2017 at 07:24 AM

Check this link for your complete answer...... http://rakeshparihar.weebly.com/my-blog/static-keyword

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Data from a simple singleton / static class return null 1 Answer

Can read but cannot pass variables between scripts... 1 Answer

Static variables assigned in the inspector 2 Answers

Static Variable crashes when accessed too fast? 0 Answers

Trying to create a static class for WWW-based functions and such 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