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
4
Question by Programmeric · Dec 21, 2013 at 03:33 AM · variablepublicprivateusewhen

When to use private var. and public var.?

Hello. I'm an intermediate programmer in Unity 3D now but guess what? I don't know when to use private and public variables!!! I know, I know, but I don't know how I didn't learn to use them. I know public var. is for other scripts coming in and out using that variable and private only that script can use it. But why would you need to use private? To prevent cheating? Also, what's the difference between normal int number : 0; and public int number : 0; I know you're probably going to scold me and say that I'm not good, but there's nothing I can do. :D

EDIT: Well maybe not intermediate but the time I've had Unity 3D. :3

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 Benproductions1 · Dec 21, 2013 at 03:41 AM 0
Share

I$$anonymous$$O I think you need to rethink you program$$anonymous$$g skill grading. There is so much more complicated stuff and I think you still need to learn basic OO (which is on the easy side of thing) ;)

4 Replies

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

Answer by Benproductions1 · Dec 21, 2013 at 04:20 AM

Hello,

Every time you write a "script" in UnityScript, Unity's UnityScript translator (Yes, it doesn't directly compile UnityScript) adds a couple things to the file:

 class FILENAME extends MonoBehaviour {
     CODE
 }

Where FILENAME is the name of the script and CODE is the code in the script. This of course only happens if you yourself don't declare a class with the name of the file.
This means that every time you write a "normal" UnityScript file you're actually declaring a new class.
Therefore the same reasons you declare a variable public/private is a "script" is the same as in any other normal class:

Obviously public variables are accessible outside of the class/script, while private variables are not. This also includes accessibility from editor scripts, meaning a private variable will not be accessible from any inspector window.
Now you're probably asking yourself why use private variables at all?
Generally there are two reasons, the latter being more valid than the other:

  1. If you're writing a library for some other developer you might want to prevent that other developer from accessing certain functions or variables that might break behaviour or cause corruption of some sort.

  2. Generally the reason you should make a variable or function private is in order to make debugging easier. If you're encountering a problem it will be much easier to narrow down if the set of variables/functions you're working with is decreased. Making specific variables/function private will greatly decrease the problem space.

You should probably make all the variables/functions private which you don't need or shouldn't access outside of the class, but none the less, private is not a necessary language feature. Python for instance does not have private variables.

To answer your last question, all variables in UnityScript default to being public, so there is no difference between public var i:int and just var i:int.

I suggest you do some reading on OO.

Hope this helps,
Benproductions1

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 Programmeric · Dec 21, 2013 at 05:21 AM 0
Share

This is the best answer I've ever gotten in here. Like, I just love it. Thanks man. I feel sorry for the other guy tho. You did good too, ArnadoTinajero, but he did better. :P

avatar image HakJak · Jul 07, 2015 at 12:58 PM 0
Share

I just want to say, I really appreciate the people that take the time to write well-thought-out and thorough replies like this. They are so helpful for learning and understanding how all of this works!

avatar image
2

Answer by ArmandoTinajero · Dec 21, 2013 at 04:02 AM

It's simple:

var appears in Unity and therefore can interact with other gameObjects or can be used to see playerHealth, enemyHealth or such.

For example, you coded a game where a player hits an enemy twice and kills it. In your code you would use (asuming you've coded the distance between player & enemy etc):

 private var Damage : int = 25;
 var PlayerHealth : int = 100;
 var EnemyHealth : int = 50;
 
 function Update ()
 {
     if (Input.GetButtonDown("Fire1"))
     {
         EnemyHealth -= Damage
         }
 }

And each of those variables would appear in Unity and you could see the effects of the script. Plus, you can change var from Unity instead of going to MonoDevelop. You could change the PlayerHealth and EnemyHealth from Unity. However, since Damage is a private var, it can only be changed through MonoDevelop and not from Unity.

private var is used when you don't need to see the effects of the script to the variable or when you don't need to change it or assign it to a object.

Hope this was helpful!

Comment
Add comment · Show 3 · 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 Programmeric · Dec 21, 2013 at 04:11 AM 0
Share

Isn't there another thing like if it is a private variable then you can't access it from an another script?

avatar image ArmandoTinajero · Dec 21, 2013 at 04:21 AM 0
Share

Yes, that too.

avatar image Benproductions1 · Dec 21, 2013 at 04:45 AM 0
Share

You're missing the key advantage of private variables, which is to reduce the problem space when debugging. Not all public variables show in the inspector, that's a completely different system.

avatar image
2

Answer by kenseiden · Jun 10, 2017 at 09:43 AM

I think it should worth noting that its a general good practice to encapsulate your variables, accessing them via methods.

For instance if you have a int health variable and it should range between 0 and 100 you'd have better control over it via method to avoid undesired results. In this example you will not only make sure your health stays between 0 and 100 but also only interact with the correct variable type.

 public class GameManager : MonoBehaviour {

     private int health = 100;
 
     public void TakeDamage (int damage) 
     {
          health = health - damage;
 
         if (health <= 0) 
         {      
               health = 0;   // character died do something
          }    
     }
 
     public void Heal(int heal) 
     {
         health = health + heal;
 
         if (health > 100) 
             health = 100;
     }
 }

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 bestrobloxgamer081 · Jun 29, 2021 at 01:38 PM

In the context of Unity, public fields are displayed in the inspector, so if you attach the Player component to a GameObject , the Health field will be visible and you will be able to edit it in the Inspector. If the field is private, you will not see it in the Inspector

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 Captain_Pineapple · Jun 29, 2021 at 01:59 PM 0
Share

i respect your will to answer questions, but please try to stick to new unanswerd questions instead of answering 8 year old questions with already accepted answers. Except perhaps if the answer is outdated due to changes over time due to Unity updates.

avatar image Hellium · Jun 29, 2021 at 03:33 PM 1
Share

This answer is a necro, but also incorrect. Private fields can be displayed in the inspector if they are serialized using SerializeField or if a custom inspector displays them.

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

C# - public variable not exposed to the Project Panel? (n00b to c#) 1 Answer

Changing variable values not having any effect 2 Answers

When issuing to print a private variable from the script, the script prints all the variables attached to objects using the same script instead of the specific object. How do I fix this? 3 Answers

Can I make variables visible to other scripts without making them visible in the Inspector? 1 Answer

Invisible Public Variable in Inspector 5 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