FindGameobjectsWithTag -> Array

GameObject Swordmen = new GameObject[10];

void Start () {
	Swordmen = GameObject.FindGameObjectsWithTag ("Swordman");

It won’t add the game objects to the array and if I try this:
GameObject swordman;
swordman = GameObject.Find(“Swordman2”);
Swordmen [1] = swordman;

It will just say: IndexOutOfRangeException: Array index is out of range.

Please help, it’s probably really obvious but I just don’t see it.
(And I’m making a enemy array where I can get one and activate it when I need it, I tried to instantiate them into the array but it didn’t work either, after it was instantiated if I tried to refer to it I got an error)

remember that c# uses 0 based arrays, ie Swordmen[0] is actually the first swordman.

how many swordmen do you have in the scene? also, make sure they are enabled in the scene.


GameObject Swordmen = new GameObject[10];

should just be:
GameObject Swordmen;

this is because you wont actually know the size of the array coming from the findobjectswithtag method.

I’ve always struggled with arrays like this in Unity too, instead I’d use
List Swordmen = new List;

void Start() {

This is pseudo code, so don’t quote me, but I believe something along those lines may work. If FindGameObjectsWithTag() returns an array itself, this might require a for loop?

Edit: Code amended - thanks go out to Uncasid.