Direct Access
1_ScriptableVariable
Last updated
1_ScriptableVariable
Last updated
If you click on the prefab_player in the scene hierarchy, you can see that he has a Health component. This component has a reference to a scriptable variable float called “example_float_playerCurrentHealth”.
Play the game, then select the example_float_playerCurrentHealth variable in your project and change its value in the inspector. You can directly modify the value of the scriptable variable in the inspector window at runtime.
You can also directly modify the variables from the Health component itself:
Moreover, by clicking on the small arrow you can expand the inspector showing the Scriptable Variable parameters. You can also change them at runtime:
While still in play mode, click on the Damage button or the Heal button, you can affect the health directly. Let’s look at the methods tied to these buttons by finding them in the hierarchy.
UICanvas/Bottom/Buttons
Traditionally, we would call Player->TakeDamage() or Player->Heal, but we can be more direct. Instead, we directly access the scriptable object variable and add a value; using the Add() public method from the editor.
It’s very direct, and you don’t always want that, but sometimes you do (especially when making Hyper casual games). For things like increasing the level index, or other simple things, it can be useful to access them directly.
In the Editor, ScriptableVariables automatically reset to their initial value (the value in the inspector before entering play mode) when exiting play mode.
The FloatVariable and the IntVariable have 3 extra properties:
IsClamped: enable the variable to be clamped.
Min: the minimum value (variable reference)
Max: the maximum value (variable reference)
Go ahead and set Is Clamped to true on the player health variable. As you can see, the minimum is set to 0. If you go into play mode and click on the Damage button or try to set the health manually in the inspector, you can observe that the health will not go below 0.
The fact that min an max are variable reference means that if you want, you could put another variable instead a value in the inspector. For example, you could bind the max value of player health to player max health like so: