Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Game Development

Daniel Hartin
Daniel Hartin
18,106 Points

public variables being accessed from outside the class, bad idea?

Before I start great course guys I am glad to see a game programming course from treehouse especially using C# which I can transfer into visual studio to make desktop applications.... when I get a bit better.

I've always been told that accessing one classes variables from another class is a big no no and in Android/Java (my most used language) you would set these variables to a private access level but create public getter and setter methods to return and update these variables from inside the class safely.

Is this something which should be done in practice but was omitted for simplicity in this case or is this a quirk of C#/Unity which doesn't apply like it does in other languages?

Be great to hear anyone's opinion

Thanks

1 Answer

Nothing wrong with doing either and in fact in most cases it's the simplest way to access other components or components on other gameObjects is just to use public (explicitly as opposed to generally where you want to use a static variable). Especially if you want them accessible in the inspector window. I honestly really don't know what's actually best practice and it doesn't seem to make a whole lot of difference other than it's quicker to simply write "public" if that's all you need.

For things like enumerators etc and evaluating them to a variable then that's typically when I use setters and getters. I've been developing in Unity full time for 5 years and really I still don't know and working with other developers there doesn't seem to be a right or wrong answer and sometimes comes down to personal preference.

I'm a lazy, sloppy coder so I'm probably not good example of best practice but I just do simplest thing of what works. I've never come across an issue or found it to be a big no no in Unity to access public variables (which is normal/most frequent way of exposing to inspector anyway).

I suppose it depends if you're talking of access in terms of write access too, if only Inspector access is required then makes sense to use [serializefield] rather than make public, but since you mention setting and getting then we're talking more than exposing to inspector, and I personally don't see anything wrong with "public" which has dual meaning of exposing to inspector too.

Pretty sure it's considered the basic "normal" way of accessing a variable from another class/component/gameobject within Unity is just to make it public. That's my opinion and I'm sticking to it, lol.

Daniel Hartin
Daniel Hartin
18,106 Points

Great answer, thanks for that. Good to hear from someone with industry level experience!!. I personally prefer getter and setter methods (I usually develop for android using Java) simply because the get and set prefix make it easier to remember what I called the chuffing things (code completion is a life saver!! and saves me switching between classes) and the obvious syntax highlighting if I try to assign an incorrect variable type by mistake, but I can see the benefit with Unity doing otherwise.