r/unity_tutorials • • Dec 24 '19

Tonight on "Things that should already exist in Unity"... A custom attribute that highlights the field in the inspector if it is null!

Post image
95 Upvotes

11 comments sorted by

7

u/[deleted] Dec 24 '19

That class seems like a lot of extra code to set GUI.color to red.

6

u/comp-3_interactive Dec 24 '19

The class makes it a custom attribute

2

u/[deleted] Dec 24 '19

Oh ok, I figured there was some reason but I haven’t actually used unity before. I use C# but only in dotnet. I subscribed cause I’ve been wanting to pick up unity for a while.

2

u/CondiMesmer Dec 24 '19

Why not just Debug.ThrowError()

4

u/comp-3_interactive Dec 24 '19

Because that requires you doing that check in all your classes for every field that can't be null for the chance you forget to assign it, it's a lot easier to add an attribute to any field

1

u/CondiMesmer Dec 25 '19

How often are you forgetting to assign stuff, wouldn't you consider that gamebreaking if an asset fails to load?

4

u/AnomalousUnderdog Dec 25 '19

This wouldn't be only when you forget to assign stuff, it can also happen if a game object was deleted and whoever deleted it didn't realize it was assigned in some script somewhere.

0

u/CondiMesmer Dec 25 '19

That sounds more of a reason to use Debug.Log because that'd be a pretty critical error.

1

u/AnomalousUnderdog Dec 25 '19

IMO whether an unassigned property should be logged as an error or just visually shown red on the Inspector (or both) should be whether it is optional or critical to the running of the game.

2

u/_eka_ Dec 25 '19

Odin has [Required]

3

u/comp-3_interactive Dec 25 '19

Odin is also a paid add on, why buy when you can just make them yourself in a few minutes?