r/unity_tutorials • • Nov 17 '21

Tip for today :)

Post image
243 Upvotes

23 comments sorted by

24

u/TMarczin Nov 17 '21

I always use [SerializeField] and properties when really necessary. I was just raised that way.

9

u/Canamla Nov 17 '21

On top of that, it includes it in the serialization process so its value is "saved".

1

u/FlyingButterGames Nov 18 '21

Thank you guys :)

5

u/[deleted] Nov 18 '21

All my homies hate non ecapualtion

6

u/[deleted] Nov 18 '21

Holy shit i had stroke re-reading that

7

u/BlueAwesomeDinosaur Nov 17 '21

Also, if you put [System.Serialize] before a class, any public or serialized variable/list created with it can be viewed in the editor.

2

u/lungi_man Nov 18 '21

Love this series. If it is a series.

2

u/Admirable_Musician_2 Nov 18 '21

I like the format of your tips. Keep it up! 👍

1

u/ImTheTechn0mancer May 14 '22

Public fields are recommended against, officially! Have standards, people!

-15

u/lemming1607 Nov 17 '21

I never use private fields, and I sleep easily at night

14

u/_Ralix_ Nov 17 '21

Yes, and your project dependency graph looks like a knitted sweater that only the Github Copilot is brave enough to debug.

Seriously though, you might get fine without it up to a certain point, but once your project becomes more complex, any debugging, change, or a fix will make you go over plenty of files, whereas if you properly encapsulate, your project will be less of a web and more of a set of building blocks you can inspect individually.

-5

u/thygrrr Nov 17 '21

Encapsulation is fine and dandy, until it isn't!

Or... could just treat your code base and its users (this includes yourself) like an adult and not read/write to and from any given public field you might come across.

Python does that, it at best has _underscore and __dunder fields as an indicator that you should treat it privately, but it respects your needs should you really want to access them.

Looking at you, Unity SRP "internal" properties for everyday camera data that prevents the normal user from writing something as simple as their own RenderObjects pass ...

Don't get me wrong, C# enables some nice encapsulation, but what I don't like in Unity is that instead of writing "private" vs "public", you need to add a pretty lengthy attribute, and depending on code style, it will go IN A DIFFERENT LINE than your field declaration... yech.

4

u/_Ralix_ Nov 17 '21

I feel you. I had to use reflection a couple of times to access some private variables in a restrictive API, so I can see how it may be useful.

On the other hand, making something public is saying, feel free to change this. Some variables are private for a reason, e.g. they don't need to be exposed and would be just clutter in your autocomplete, they determine the internal state of the object and changing one without changing others breaks the object, some should always fall within a certain range (0.0-1.0 percentage) etc. And then you have to dig into the code to see what the public variable actually does and how to assign to it properly.

Again, small projects will likely be fine, large projects also with coworkers unaware of the exact structure of your code may easily grow harder and harder to maintain.

-7

u/lemming1607 Nov 17 '21

Nope, doing fine, and sleeping great

2

u/likely-high Nov 18 '21

Let me guess never completed a decent sized project.

0

u/lemming1607 Nov 18 '21

You'd be wrong.

Just because they're public doesn't mean you need 5 classes setting public setters

0

u/likely-high Nov 18 '21

OK what's the steam page/app store for this project?

How many years experience do you have in software development?

I suggest you Google SOLID and OO principles. I'd hate to imagine your codebase.

0

u/lemming1607 Nov 18 '21

I'm not gonna doxx myself.

Roughly 5 years

Well aware of solid and OO

3

u/burros_killer Nov 17 '21

How's your coworkers? Haven't knock on your door with a shovel handle yet? :)

7

u/djgreedo Nov 18 '21

How's your coworkers?

Nobody who makes everything public is programming in any kind of professional/collaborative capacity.

I'm self taught and used to make everything public because it was easier. Now I know better, and understand that keeping code structured and encapsulated is an investment for future me to avoid bugs, headaches, and time.

Unfortunately, a lot of Unity tutorials use public access where [SerializedField] is what is actually needed, and that spreads through Unity devs who are often self-learning.

1

u/burros_killer Nov 18 '21

I'm self taught either. Never get how unstructured mess is easier. You'll have to read it sooner or later. And if pause before that is long enough that's a restart of a project. As for "no coworkers" - that pretty self evident, my bad))

3

u/lemming1607 Nov 17 '21

Solo dev

3

u/burros_killer Nov 18 '21

Yeah, I should've figure it out myself :)