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
9
Question by Wollowizard · Apr 08, 2011 at 09:50 PM · variablestaticpublic

Whats the difference between public and static variable?

As question says whats the difference?

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

Answer by Wibbs 1 · Apr 08, 2011 at 09:59 PM

static - the same variable is shared by all instances of the class that are created, and can be private, protected or public.

public - a seperate variable for each instance of the class, and it is visible and accessible from outside of the class.

Comment
Add comment · Show 4 · 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 Wollowizard · Apr 08, 2011 at 10:03 PM 0
Share

Very rapid and useful response thank you very much :-)

avatar image jellyberg · Jun 21, 2013 at 07:09 AM 1
Share

How would you access static variables from another script? I've tried scriptName.variable but it doesn't work.

avatar image Bunny83 · Jun 21, 2013 at 08:04 AM 1
Share

@jellyberg: Is your static variable public? This answer might be a bit misleading. The OP compares a public variable to a static variable which makes no sense since static and public are two different things.

static means there's only one (static) version of a variable which belong to the class and not to a specific instance.

public is an access modifier which controls the visibility of members.

To access a variable from outside a class the variable needs to be public.

avatar image jellyberg · Jun 21, 2013 at 02:43 PM 0
Share

@Bunny83 Ah right, so I would declare my variable with public and static before it - thanks, that works perfectly!

avatar image
12

Answer by RaphDuBus · Apr 08, 2011 at 10:12 PM

As i undertand it :

  • a public variable will be accessible by other scripts
  • a private variable will not be accessible by other scripts

public & private var are unique for a script instance.

  • a static variable will be shared betwenn all the instances of your script.

For more details : http://answers.unity3d.com/questions/47565/public-private-and-static-variables-in-js

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Public variable not showing in Inspector(Solved) 1 Answer

How to make variable accessible by other scripts but not visible in editor? 1 Answer

GUI.DrawTexture Error & Static Variables!! 2 Answers

[C#] Is there a way to call static variables directly, independent from parent class? 3 Answers

Help With C# Static Boolean! 2 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