Hello everyone, I have an issue in am trying to figure out. So right now on my player I have a script that handles spawning different player models, which are all contained in a dictionary that holds their name as the key and the game object as the value. What I’m trying to do is have a UI scroll view that holds all the models as individual buttons with their pictures and when you click one, it spawns that prefab. What I’m stuck on is how do I have the button know which prefab to spawn? The actual spawning code is held on the player, and when you obtain a model it adds itself to the list, so I don’t know how to get them to communicate. Sorry if its a dumb question, but I’m stuck
Your buttons need to have a way to reference the key so when you trigger the OnClick event, you can then access your dictionary, pull the prefab and instantiate it.
I was thinking about something like this, but I don’t know how to go about it, since the buttons are instantiated when you “collect” the model. I have a way for them to change the text and picture so you know what you have, but that’s as far as I got.
Totally reasonable question.
When you reach a certain point you want to start breaking things apart.
There should be a repository of things you can make right now. It might be as simple as a Resources/ThingsYouCanMake directory with prefabs that are loaded once per level. It has no UI and it has no way of making things. It’s just identifier1, thing1, identifier2, thing2, etc. ScriptableObjects are AWESOME for this sort of predefined “property.”
You would create UI dynamically based on what is available in that repository when it is time to ask the player.
The UI would contain entries with the picture and some unique identifier that the repository knows “Ah, you want one of these.”
When a player intent comes in (tap of button, press enter, whatever), the UI would send an intent (“spawn this identifier1”) to a spawn manager.
From what came into the spawn manager from the UI, the spawner would look up the identifier in the repository, pluck out the prefab and make it.
Thanks for this, I’ll take the concepts and try and make it work!