- Home /
My question and answer were not very good
Spawning random prefabs with the same tag
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
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?
i want them to spawn at a single point. I dont care if there are duplicates as long as they are random
Answer by TimBorquez · Mar 25, 2013 at 06:08 PM
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"));
}
Answer by sparkzbarca · Mar 25, 2013 at 04:06 PM
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
thanks, i dunno exactly how to do everything you said but imma give it a shot
ok im still having trouble actually executing this, can someone help
This sounds like what I want.. But itwould have been nice if you used some actual code.
Follow this Question
Related Questions
Is there an easy way to apply the same tag to all children of an object? 4 Answers
How to spawn a new prefab when the last one was destroyed?? 2 Answers
How do you spawn multiple prefabs within a certain distance from each other from one spawn point? 2 Answers
prevent object from falling to the left side when instantiated (spawned) ? 1 Answer
Prefab tag bug 0 Answers