So in general I tend to not trust user input or input to public methods when I write my code. I tend to check for out of bounds, for null values, etc and try to handle these cases. [I’m not an engineer, but it’s a habit I picked up from 20+ years of listening to engineers work] Here is what my code looked like before this lesson. So the good news is I would have caught this problem early. The bad news is my code has no graceful fallback and it would have been awesome to find this mistake before running the code.
Here’s my question: This lesson helped us solve this instance but I am wondering if there’s some generalizable lesson here? We learned a tiny bit about editor scripts earlier in this course. Are editor scripts a good way to check if you’ve configured something stupidly with your editor references?
Related: I’m thinking of giving all my weapons a “weaponType” enum and making sure that for a given weaponType (e.g. unarmed melee, unarmed magic, melee weapon, ranged weapon, magic staff, etc) that all appropriate fields are set for that type. For example it would be AMAZING if the inspector tab could tell me that some object or component is not set up properly.
if (animatorOverride != null)
{
animator.runtimeAnimatorController = animatorOverride;
}
else
{
if (name != "Unarmed")
{
Debug.LogError($"Attempting to spawn a weapon called {name} with no animator override!");
}
}