im lost, i know it would involve instantiating but i need help
what im trying to do is spawn random prefabs ive made with the tag “toy” when there is a small enough amount of objects tagged with toy
i made an if statement so once theres less than 10 things tagged toy it would make more randomly, but i dont know how
toyCount = GameObject.FindGameObjectsWithTag ("toy");
if (toyCount.Length < 10){
//here i would instantiate "toy" tagged items randomly
}
thanks
2 Answers
2
take the prefabs and collect them all up in a list gameobject.findallwithtag
that’ll return a list toss that in a newly created variable thats a gameobject list
use random to randomly pick an object.
use it to fill another list this is a list of objects that actually exist
fill that list, while it’s size is less than x keep spawning.
as you destroy they’ll be respawned. once its size == x stop spawning
ok i think i may have just discovered that i cant find and spawn things using findallwithtag if they arent in scene
ive now placed my objects in Resource folder
so now im still dealing with the issue of spawning them randomly from that folder
toyCount = GameObject.FindGameObjectsWithTag ("toy");
if (toyCount.Length < 10)
{
var instance : GameObject = Instantiate(Resources.Load("fake toy"));
}
How many? Do you care if you get duplicates of some of them, or must an item from 'toyCount' be unique? How should they be positioned and rotated?
– robertbui want them to spawn at a single point. I dont care if there are duplicates as long as they are random
– TimBorquez