G
G
GoldeNKingMiDaS2021-12-19 23:09:48
Unity
GoldeNKingMiDaS, 2021-12-19 23:09:48

How to dynamically create many similar items in the scene, but with different data?

I am making a 2d game. And the crux of the matter is this, I have an inventory and I want to be able to throw things out of it. But in order to create something like a "backpack" on the floor. The whole difficulty lies in the fact that the script can only create objects from the prefab. And by changing the value of the inventory data of this "backpack" it will be applied to all items on the stage since the prefab is one. Now I have these "backpacks" when they are created, they have a random number in the name, which I can save in an array to save and load the game. But storing data about the entire object in one line is the most inconvenient. I wanted each created object to have its own array of items in order to reduce the scale of its elements to individual slots. I was able to do this on pre-created objects in the editor, with different names, but with one script. Maybe there is a way to create an object not from a prefab, but as in the editor? Or are there simpler ways how can I implement such storages on the ground, which will be dynamically created during the game from a script? We also need the ability to change the values ​​on the object so that you can put items in an already created backpack or take it from it to the inventory.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
zukrac, 2021-12-23
@GoldeNKingMiDaS

At the moment the backpack falls out, you decide what it will contain.
1. you put specific items in it at the stage of creation (scene or prefab). I assume there is such a possibility.
2. or you put in it dynamically. For example, through pseudo-items that are actually descriptions of the item, and not the item itself. For example: type=sword, level=80, rest=random. Or a collection of a set of items.
In short, everything is through the code. At the moment the backpack falls out, you determine what is contained in it. It remains only to determine how it will be convenient for you to do it.
Usually they make a base of items. their collections. Selections with a filter. And item generators - this allows you to fill any backpack.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question