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

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

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.

thx for posting answer

thx for posting answer

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"));
	}

please don't post answer as comment