Most tutorial need to drag and drop a gameobject into the script component do be able to use it’s sprite/image.
But I need a dozens of sprites as reference.
So I want to create an game item (Prefab) as template, this item generated multiple times at random position on game world.
And it’s sprite will changed to specific image, depending on what “type” variable prefab given. maybe a prefab item have “fruit” or “vegetables” variable.
It’s not pure random, I want to control rare items and generic items.
In Phaser javascript framework, I can change sprite easily like this
gameObject.setTexture(“sprite_key”);
in Phaser, all sprites have it’s predefined key, so I can control it easily.
How I can achieve this in Unity ?
I’m not sure if I understand exactly what you’re asking. But, no matter what you do, you need a reference to the gameobject, or, in this case, the proper component (image, sprite, whatever you want to access).
Now, this doesn’t mean drag and drop in the inspector. But, if you instantiate a prefab, thus creating a clone, you will need to at that point, grab the image component using GetComponent() and then targeting the sprite on it.
Or, if you’re using a 2D sprite, then you change it through that component. The end result is, access the proper components and set those values.
However, you could also simply have a script on the prefab that has a reference to the proper component and let a method handle setting the sprite.
There are so many ways to accomplish what you want.
I would suggest looking through some tutorials on Unity and components.