How to create a prefab instance that is different from others

I’m creating a clicker. I have a prefab of the building, which is a button. In my script, I have a list of structures containing data that are inserted into the prefab and then the prefab is added to the scrollview. The main function is to buy buildings to increase the amount of resources received per second. However, I want some buildings to also provide new functionality, such as crafting. How can I do this? In general, is it wise to use a prefab here, because I also have a prefab of resources, and a certain instance of the building should be responsible for a certain instance of the resource.

I mean you have prefab variants, which is something to look at if you don’t know about them already.

But a clicker game should be leaning heavily into a model-view approach. The game state should be expressable as pure data, separate from any game objects. And then you just build a visual representation based on this data, and continue to update it accordingly.

I would not be mixing the actual qualities of a build into the prefabs themselves. Though ought to be just visual representations of what the underlaying state has. Mixing all this data into game objects is going to make save games hard to implement. And if you’re making a clicker game, you should should be thinking about your save games every step of the way.

1 Like

I’m sorry, but I didn’t understand what you mean. :frowning_face:

Right, apologies, it was rather general advice.

To answer this more specifically:

This really depends what ‘such as crafting’ entails. Like a new UI menu for the player to interact with? I suppose if you are just instantiating a prefab, this prefab has some component or method on it that can be called to “unlock” features, or has the appropriate components on it as to open up a crafting menu. Though ideally, it’s simply a data object that factories another bit of data, of which may express interfaces or override methods from a base class that can perform these operations.

It should probably record this fact in the game’s save data, so this state can be restored later on.

In any case, it is a broad question and hard to give specific advice to. A lot of this comes down to the particulars of your project.

I want to create a button that opens the crafting menu when clicked. For example, in the cookie clicker, when you buy buildings, a sign with this building appears, and you can click on some of them to access other functions.

My main problem is whether it’s right that I use prefabs, because we use them to create copies of an object, but in this case we want some of the copies to have their own unique methods, so I had doubts about the appropriateness of using prefabs. But on the other hand, if I have to give up prefabs, it means that I will have to create similar objects in the editor, some of which have different fields, as a result, I am very confused. Now I’m thinking that maybe you can use enum and, depending on the value, pass a certain method or not pass anything.

Regarding your first message, did I understand correctly that you recommend using the MVC pattern? That I need to create a separate class (Model) that will work with data (for example, it will be able to take saved data from a file and transfer it to the View class), and the Controller class will be responsible for clicking on buttons and updating data.

I mean for the in-game scene interactivity, yes you probably just want to be spawning prefabs with right components/functionality on them. That goes without saying.

These prefabs might just be holding onto their own unique data, or referencing data from the overall game state data blob. How to make them different? Really might just be a situation to add/remove components on them. If these prefabs need to be referenced in a common way, a core or base-type component might be an option here.

More so just meant the model-view pattern in general. Ergo, keeping the game state separate from the visuals. Whether that ends up being MVC, or MVVM, or any other similar model-view subset, doesn’t matter. In an ideal situation, your whole game state could be serialisable, so you just need to dump out the entire game state data blob as a save game. Then you read this at a later point in time, and use that to rebuild the visuals.

1 Like