- Home /
How to get around hard-coded string in Unity?
Most of the strings in Unity are hard-coded. For example, Button "Fire1" I don't like that we hard code the string "Fire1" inside the script directly. One day, I might change the Project Settings of that Button and I have to go and fix all of the hard coded strings. This includes Animation Triggers, Game Object Name, Tags, etc.
What is your solution about this problem?
Answer by Bunny83 · Aug 18, 2016 at 03:23 AM
Uhm, you can simply use public constants
public class Settings
{
public const string FireButtonName = "Fire1";
}
So you can simply use Settings.FireButtonName
instead of "Fire1"
O$$anonymous$$. That is what I was thinking. By the way, the variable should be static for you to call it like that.
Uhm, no, that's not a variable, that's a constant. Constants are always static. You can't use the static modifier on constants.
O$$anonymous$$. Sorry. I'm not familiar with C#. I thought it would be like Java.