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
0
Question by sdgd · Jan 27, 2013 at 10:28 PM · staticglobal

what does Static Function exactly do?

correct me if I'm wrong

well I've heard that if static it gives variable being global in the range that's allowed

if static only 1 exist and classes are made to have loads of them what's difference between static class/function

if it's wrong that I'm asking 2 Questions please tell me and I'll make 2 Thread Q

  • public = global

  • private = local

    • static = global (only 1 exist)

    • public static = global (only 1 exist)

    • private static = localized global (only 1 exist)


what's difference between

  • Static Class

  • Static Function

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
3
Best Answer

Answer by Bunny83 · Jan 28, 2013 at 01:54 AM

Basically all functions are actually "static" because the code of the function only exist once in the whole program. The difference is that member functions which "belong" to an instance of the class have an additional invisible parameter: "this".

If you call a member function you actually just call the function and the compiler implicitly passes the reference of the instance.

Static functions are functions without that implicit parameter, that's all.

If you use C# you might know "extension methods". They actually are implemented as member functions actually wotk.

For example:

 //C#
 public static class Vector3Extensions
 {
     public static Vector3 Scaled(this Vector3 aThis, Vector3 aScale)
     {
         return Vector3.Scale(aThis, aScale);
     }
 }

this extension would allow this:

 Vector3 myVec = new Vector3(2,4,8);
 Vector3 result = myVec.Scaled(new Vector3(2,3,1));
 // result will contain (4, 12, 8)

When Scaled is executed the actual "context" object will be passed as additional parameter.

Inside "real" member functions the compiler provides additional syntactic suggar. Since your function belongs to a class instance the compiler interprets all variables it don't know as "this.varName"

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 Golan2781 · Jan 27, 2013 at 11:20 PM

A static class is simply a class with all its member variables/functions being static. If a static class has a function, that function is automatically static as well.

A static function does not require an instance of a class, similar to how a static variable does not either. Generally, it will only work with static variables of the same class or variables from other classes.

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 sdgd · Jan 28, 2013 at 12:09 AM 0
Share

but static class does not require an instance either

so what's difference between static class and function

I see no difference

what more can static class do than static function?

avatar image Loius · Jan 28, 2013 at 01:06 AM 1
Share

A static class prevents you from accidentally adding a non-static member, and also prevents you from accidentally creating a new instance of it using the 'new' keyword.

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

12 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

Related Questions

Is there any way to edit variables inside other scripts without declaring them as static? 1 Answer

Prefab's Script affecting all the Prefab 1 Answer

Update Function Won't Do If Statements 1 Answer

Adding static functions 2 Answers

Static variable issue in published 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