Scene Setup
8_RuntimeVariables
Last updated
8_RuntimeVariables
Last updated
In this example, I have multiple enemies, each with a health bar. The health and health bar of enemies are generic components that rely on a Scriptable Variable float (Health) for their logic.
When you play the game, each time an enemy hits the player, the enemy will lose health. You can see their HP bar updating. When an enemy dies, it will disable itself.
Letās select an enemy to understand how this works. Go to the scene hierarchy and click on an enemy.
The enemy script is very simple; it detects collisions and calls a method on the RuntimeHealth.cs component. This component is the one we are interested in. You will notice that the āRuntime HP variableā field is left empty! Now, if you open the script RuntimeHealth.cs, you can see that in Awake(), we create a runtime variable if the field is null. You should use the SoapUtils.CreateRuntimeInstance<T> to do so.
Once the variable instance is created, we pass it to the health bar. Because the HealthBarSprite is a child of the āprefabā, we can safely use a direct method call. However, if the Health bar were in 2D on a UI Canvas (maybe in another scene), it would be better to use events to decouple these systems. The execution flow is extremely important when using runtime variables. You will notice that I am not relying on Awake() for the Health bar component. Doing so might cause a race condition between the Awake() of the RuntimeHealth (which creates the instance) and the Awake() of the Health Bar that registers to it.