Private vs public

Hi, I’m just wondering what’s the difference between private and public? When to use or not? I’ve researched about it and I don’t quite get it. I know the difference but I keep picturing other scenarios and I don’t understand when I will use it.

I mean, based on the code, private enum States can be called everywhere.

Thank you for your insight on this. Sorry, noob here. =)

Cheers!
Sig

Typically the rule of thumb should be, keep everything private until you have to expose something, e.g. when you find a reason to expose a property or method.

2 Likes

@Rob is right with the best practice. To try to help you understand why, let’s look at an example.

Let’s say I have a class ScoreManager which takes care of all scoring functions in my game.

If it has a public field score, any code anywhere in the game can update the score by getting a reference to the active ScoreManager and calling ScoreManager.score = (there’s some subtlety here with instance names but you get the idea).

This is generally undesirable, because there are other things the ScoreManager might want to do when adjusting the score - logging, firing Events, firing Analytics, etc. You want to control access to the score field to force it to update the way you want to.

So you make it private, which locks score down to only the ScoreManager class. You might provide methods such as public void IncrementScore(int) to allow other classes to suggest a score update (rather than changing the score directly) and you then have control in IncrementScore over how the ScoreManager chooses to do that.

Of course if another class just wants to know the score but not change it, you’ll need to provide a method public int GetCurrentScore() or similar. Another way to do this is to declare the score as follows:

public int score {get; private set;}

This allows any class to read the score, but only the enclosing class to change it. I mostly use this form in my code for fields unless I need to make them public to expose them to the editor.

Hope this helps

4 Likes

Great example @ninjachimp :slight_smile:

Hi @Rob, thank you for the advice. I’ll do that. =)

@ninjachimp, thanks for explaining and for the example. I’m sorry but you lost me in the 3rd paragraph. I don’t know what is classes and methods but I’m looking it up. I’ll understand it soon. haha. Thank you!!!

Thank you for your time!

-Sig

2 Likes

The use of public in one place and private in another threw me a bit at first too. I think the important difference is that the ‘text’ field has to appear in the Unity Inspector window, so that you can drag the text field from Canvas across to it. Public variables appear in the Inspector whereas private ones do not.

Hopefully scoping of variables turns up again in a later lecture.

Personally I think it would have made things clearer if the field had been called something like ‘gameText’ rather than ‘text’, as I found that confusing.

Only 3 months late for the thread! :wink:

My understanding (from this early stage) was that a public field is accessible throughout the project as a whole, whereas a private field would only be accessible to the specific script it’s declared in, which will come up later on when multi-script projects start appearing. (Or at least the class they’re declared in, which for people in this stage of the course is the same thing.)

Am I off base there? Or would even public fields only operate within that particular script?

Spot on.

This may be of use, check out the links within also;
https://msdn.microsoft.com/en-us/library/wxh6fsc7.aspx

1 Like

It may indeed - thanks!

1 Like

Privacy & Terms