How to select an item from an GameObject array just ONCE?

Hi guys! I apologize if this questions have been already posted!.. But I guess I’m not good at searching!

Anyway, this is what I want to do: I created a GameObject array. I select the items of the array via Inspector. When my game is running, a GameObject (Character) is selected and it has to walk up to a target.position.

The problem I have, is that the character that have been already selected, is again selected! What I want to do is to avoid that! So, How can I select an item from an array just once? Please, C-Sharp!

Here is the code I have so far:

using UnityEngine;

using System.Collections;

public class RandomCharactersMovement : MonoBehaviour
{

public GameObject[] Characters = new GameObject[5];
public Vector3 Target = new Vector3(-18.03273f, 0.09936082f, 1.479195f);
float smooth = 3f;
public int TimeOut = 7;
public GameObject characterChosen;


	
// Use this for initialization
void Start ()
{
	RandomCharacter ();
}

// Update is called once per frame
void Update ()
{
		Invoke("MovingCharacter", TimeOut);
	
}
void MovingCharacter(){
	if (characterChosen.transform.position.z <= 1.910322) {
		characterChosen.transform.animation.CrossFade("walk");
		characterChosen.transform.Translate(Vector3.forward * Time.deltaTime * smooth);
	}
	else {
		characterChosen.transform.animation.CrossFade ("idle1");
		Invoke("GoBack", 10);
	}
}
void GoBack(){
characterChosen.transform.eulerAngles = new Vector3(0,180,0);
characterChosen.transform.animation.CrossFade ("walk");
characterChosen.transform.Translate (Vector3.forward * Time.deltaTime * smooth);
	Invoke ("DestroyIt", 5);
}
void DestroyIt(){
GameObject.Destroy (characterChosen);
}

void RandomCharacter(){
	int characterIndex = Random.Range (0, Characters.Length);
	characterChosen = Characters[characterIndex];
}

}

Yes, as @BLiTZWiNG said, you should use a List in a way known as a shuffle bag. This removes an element of an array after having being selected, so as to eliminate reselection.

To do this, at the start of your script (after the line using System.Collections; add this line:

using System.Collections.Generic;

That allows you to use the Generic class. Then, replace your Characters variable with:

public List<GameObject> Characters = new List<GameObject>();

From there you can add all your character gameObjects to the new List Characters in the inspector. And then finally, at the end, rewrite void RandomCharacter() as:

int characterIndex = Random.Range (0, Characters.Count);
characterChosen = Characters[characterIndex];
Characters.RemoveAt(characterIndex);

Hope that helps, Klep

You should use a List and then once you select a cahracter, remove it from the list.

That’s awesome!! Thanks so much your answers!!

I’ll try it as soon as possible and I’ll post you the results!

Thanks again Guys!.. I’m starting to like this site a LOT!