How I deal with string literals

I just make a SerializeField with a Header called string literals and copy the component name in the inspector and assign it there with a Tooltip reminding me where it’s used in code and for what purpose. I probably could be more specific and find a drag and drop solution but this is simple for me to remember and implement without too much hassle for where I am at now.

If it works for you and is simple, then absolutely go for it! :slight_smile:

If you have ever heard of enums, you could see if that helps make your approach even simpler: advantage over string is that, in the inspector, they appear as a dropdown menu - which is more convenient than copy/paste a name I think - but also are much less error prone around things like typos (and are more reusable)…

… maybe that could be something of interest to you…

1 Like

I always forget about enums if I am being honest lol, but yeah that would definitely be more efficient and just as easy to implement.

I do it like this:


       private readonly int ATTACK_TRIGGER = Animator.StringToHash("Attack");

        private void StartAttackBehaviour()
        {
            animator.SetTrigger(ATTACK_TRIGGER);
        }

Ideally one could have one script file to define all those animator parameters as well as other similar occurences of strings, or use (an) enum(s) for all hash values.
(So maybe you would want to have one group of animations that are all related to character movement and combat, or one for movement and one for combat actions, and you could have one for other types of animations that are not part of the character animator…)

But for now I will just stick them into the scripts where they’re used since at this point there isn’t enough of the larger structure of the project in place…

1 Like

This looks good.

Privacy & Terms