- Home /
random food spawning
Hey, i want to make it so that an empty game object loads a random prefab with the tag "food" so if i make 3 prefabs with the tag food, when the game starts the empty game object will replace itself with a random object from the three objects with the tag food so when the game starts there will always be a random food you get.
Please help, preferably in JavaScript.
Answer by dorpeleg · Oct 24, 2012 at 01:47 AM
I will not write the code for you but I can point your in the right direction.
This is the code you should use for placing a new object in the sceane: Object.Instantiate
This is the code you should use to find objects with tag: GameObject.FindGameObjectsWithTag
If you are straggling with specific parts of the code, we can help you, but I don't think anyone will write the entire code for you.
Answer by Hybris · Oct 24, 2012 at 06:22 AM
I wouldnt use prefabs, I would make a list of the objects and randomly choose from that list.
so the variable would be: `var food : GameObject[];` if I remember correctly then you should use: `Random.Range(0, 5);` you could also use a variable for those. If you want to know how much objects you have in the list, do this: `print(food.lenght);` that' s it
Your answer
Follow this Question
Related Questions
Creating a light in game C# 1 Answer
Randomly Initiate Cubes - Prevent Same Position 0 Answers
Room generator spawn 1 unique room 1 Answer
How do I spawn a random prefab? 1 Answer