- Home /
Multiple technical questions, lack of context
How to generate random object with attributes?
Hello all.
I'm working on a 2D game where i need a new object to be generated every few seconds somewhere in the screen. This object can be one of 20 items, divided into 4 categories (cat1, cat2, cat3, cat4). Each category has 5 different images to pick from, but technically all objects in cat1, for example, behave identically except for the image file attached to them. I've read that I can use an Instantiate method to spawn my objects, but I'm not sure how I would go about setting it up to pick a category and an image from it.
So far, I've managed to instantiate my object in a random position (restricted a bit):
var myObject : Transform;
function Update()
{
Instantiate(myObject,new Vector3(UnityEngine.Random.Range(-3f,3f),UnityEngine.Random.Range(-3f,3f),UnityEngine.Random.Range(-3f,3f)), Quaternion.identity);
}
However, this is using the gameObject with a preset image for its sprite via the Sprite Renderer component. Ideally, I'd want it to randomly select an image from my assets folder.
Any help would be much appreciated!
There are multiple technical questions here, and there is not enough context to give you an accurate answer to the most obvious one...how to change a texture of a game object. Unity Answers addresses a single, specific technical question.
To start, research the many ways the texture can be change. Do some goggling on the follow:
$$anonymous$$aterial.mainTexture
sprite sheet
texture atlas
uv coordinates
material.mainTextureOffset and $$anonymous$$aterial.mainTextureScale
texture array
Resources.Load() and Resources.LoadAll()
Sprite
Follow this Question
Related Questions
Object Spawning Randomly 0 Answers
Why do objects keep spawning on top of one another? 0 Answers
Random select from array and spawn 1 Answer
Creating a light in game C# 1 Answer
How can I align instantiated prefabs randomly on runtime #C 0 Answers