Adding & Removing Elements
3_ScriptableList
Last updated
3_ScriptableList
Last updated
In this example, we made a scriptable list of type Player. Find the example_playerList asset in the project.
Obvious/Soap/Examples/Content/ScriptableVariables /
In the scene, we are going to spawn players and then destroy them. This behavior happens in the PlayerSpawner.cs. Using the 2 buttons on the screen (Spawn and Destroy) we are calling methods on this class.
If you check the prefab_player, you will notice that the Player.cs has a reference to this list.
It adds himself on Awake() and remove itself OnDestroy().
You can reference this list in other classes and access it without needing to know how it is populated, and without needing a “manager” to hold it.
If you select the ScriptableList in the inspector, you will see the list of players in real time.
By clicking on the first button (with the name of the object), it pings the object in the hierarchy. By clicking on the “Select” button, it will select the object in the hierarchy.
You can also call common methods that you would call on a normal list, like Add, Remove, AddRange, RemoveRange and Clear and execute iterations (index or foreach).
Moreover, by clicking on the small arrow you can expand the inspector showing the Scriptable list inspector.