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. :smiley:

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


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 {

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,

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!

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;

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