How to instantiate from random objects that use the same tag.

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 :slight_smile:

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.

This is pretty basic stuff… any tutorial is going to have it.

Put the items in the inspector:

public GameObject[] TheThings;

Pick randomly what to Instantiate():

int which = Random.Range( 0, TheThings.Length);

Instantiate it:

GameObject copy = Instantiate<GameObject>( TheThings[which]);

Now do stuff with copy: move it, rotate it, etc.

1 Like

Since the enemies appear at random times too they should be found before spawning. For now i use

GameObject[] Enemies;
Enemies = GameObject.FindGameObjectsWithTag("Enemy");

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 :slight_smile: Thank you i will try this solution right away.

1 Like

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.

To pick a random rotation:

Quaternion rotation = Quaternion.Euler(
  0,
  Random.Range( 0.0f, 360.0f),
  0
);

To use them in my original example above:

GameObject copy = Instantiate<GameObject>(
   TheThings[which],
   position,
   rotation
);

(Broken into lines for clarity and readability on the forums)

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 :slight_smile: I am sure it will

The tag is merely a property of a gameobject which essentially gets fully copied at Instantiate()

If you need it to no longer have that tag, you can change it after the object has been instantiated.

Thanks i solved it all and it’s working great i will soon post my solution.

1 Like