- Home /
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
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) ;)
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:
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.
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
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
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!
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!
Isn't there another thing like if it is a private variable then you can't access it from an another script?
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.
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;
}
}
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
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.
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.