public float hp = 15;
[FormerlySerializedAs("hp")]
public float health = 30;
Why do I have two variables now? Shouldn’t I have only Health: 30, since I’m just renaming the hp variable to Health and setting it to another value (from 15 to 30)?
You used that attribute completely wrong. You can use it when you rename your variable to keep the old, already serialized value. So if your old script looked like this:
public class YourClass : MonoBehaviour
{
public float hp = 15;
}
So when renaming the variable to “health” you would do
public class YourClass : MonoBehaviour
{
[FormerlySerializedAs("hp")]
public float health = 15;
}
So when Unity deserializes the class from the serialized data it will read the old hp value and load it into the health variable. Note that renaming means that the old variable doesn’t exist anymore. In your code you still have both seperate variables.