Say i have 6 gameobjects with a name tag Enemy. I want to spawn (instantiate) a game object from one of those 6 game objects. So every time a spawn happens it should look how many Enemies there are and pick one to spawn from.
Does anyone have a clear script example of this. Any help on this would be appriciated.
Thank you for your time
You likely have some script spawning the enemies. Just have this script keep track of them, ie keeping them in a list. When an enemy dies it should be removed from that list.
If you dont want to manage the list yourself (which would imho be the proper approach), you could try gathering all Enemy-tagged object by using FindWithTag. I personally recommend against using any of the ‘Find’ methods tho, as they are rather performance hungry and unreliable (in terms of typos, working with strings, …).
Now you simply take the length of the list, calculate a random number and select that enemy to work with.
I need to know and understand how to script the Intantiate line for pciking a random object from the Enemies
Instantiate(Random object from enemies, position and rotation)
Thx Kurt i see i was in the right direction (post it before i saw your post) I searched through many tutorials but they seem to skip this part Thank you i will try this solution right away.
See my previous post for picking which one to copy.
To pick a spot on the ground, cheesy and easy:
Vector3 position = new Vector3(
Random.Range( LowestXValue, HighestXValue),
0, // or height of ground
Random.Range( LowestZValue, HighestZValue)
);
Or have a bunch of random GameObjects as potential spawn spots, put them in a list the same way (or by FindObjectsWithTag) and choose one randomly the same way.
Kurt thank you for your help so far. If I am correct this script makes a copy of one of those tags objects right ? But i want one of those tags to instantiate a new gameobject (other tag) Perhaps its me not getting the full logic of it all yet I am sure it will