I’m writing some code to create buttons that will allow the player to build some in game objects. Each gameobject starts as s Scriptable Object, which contains the gameobject’s base stats and a prefab. During initialization the Scriptable Objects are created by deserializing some JSON, following this they are added to a List.
The Scriptable Objects all derive from the same base Class “RoomTemplate” but there are many child classes such as LoungeTemplate and KitchenTemplate. All of this works just fine.
The problems start when I try to create the GameObjects. I populate a scroll rect with buttons by iterating through the List and when I click them it just runs the Create method in the parent room template object. I understand that this is because by placing them in the list I have Upcasted them but since they’re created by iteration I can’t think of a good way to Downcast them with resorting to a long list of if statements or a switch statement that identifies the upcasted class and downcasts it to the correct one.
Can anyone help?