How to determine and delete the oldest instantiated object?

I am new to unity and programming.

I am creating a waterfall of letters that is destroyed upon typing the corresponding letter. But if there are multiple instances of a letter, every letter of that type is destroyed. How can i track and delete only the oldest one?

I am not looking for complete code, just a point in the right direction, I have looked into List and arrays but I can’t wrap my head around how to.

Well, it’s pretty easy. Basically there are two ways:

  • time-based (so each object lives for a certain amount of time eg. 5sec)
  • count-based (there are only a certain number of objects allowed.)

For a lifetime limitation all you need to do is call Destroy right after you Instantiated the object but make sure you provide a [timeout value][1]:

// C#
float timeToLive = 5.0f;

public Transform CreateObject()
{
    Transform clone = (Transform)Instantiate(prefab);
    Destroy(clone.gameObject, timeToLive);
    return clone;
}

For a count limitation, just add your objects to a List and check if the list count is larger than your desired threshold and simply destroy the first object in the list (and remove it) until the count is within the desired limit:

//C#
int maxObjCount = 20;
List<Transform> objs = new List<Transform>();

public Transform CreateObject()
{
    Transform clone = (Transform)Instantiate(prefab);
    objs.Add(clone);
    while(objs.Count > maxObjCount)
    {
        if(objs[0] != null)
            Destroy(objs[0].gameObject);
        objs.RemoveAt(0);
    }
    return clone;
}

edit
Ok, if you have a stream of different letters and want to destroy the oldest letter “X” there are also several possibilities. One way is to hold a List for each “letter type” in a dictionary for example. That way you can easily access the “correct list” for a certain letter. In this case you need to put new letters in the “correct” list.

Another way would be to have only one list which makes the bookkeeping of the objects simpler but the searching a bit more complicated. This is probbly the easiest way. Even with 1000 letters on screen it’s no problem too search through 1000 objects since you only do this when the user presses a button.

To identify the letter-type of an object you could simply rename the cloned object so it’s name reflects it’s letter:

// To create an object
// C#
void CreateLetter(string aLetter)
{
    // If you have seperate prefabs you have to select the right one for "aLetter" here
    Transform clone = (Transform)Instantiate(prefab);
    clone.name = aLetter;
    objs.Add(clone);
}

// To remove the first letter
// C#
void RemoveLetter(string aLetter)
{
    for(int i = 0; i < objs.Count; i++)
    {
        if (objs*.name == aLetter)*

{
Destroy(objs*.gameObject);*
objs.RemoveAt(i);
break; // This is important to stop the loop once the first is found
}
}
}
_*[1]: http://docs.unity3d.com/Documentation/ScriptReference/Object.Destroy.html*_

Well, if you’re concerned about performance, you could create a hash map of letter names => ArrayLists of letters.

var letters : Hashtable = new Hashtable();

function Awake() {
  alphabet = ["a", "b", "c", "d" ... etc ];
  for (letter in alphabet) {
    letters[letter] = new ArrayList();
  }
}

Then, the approach is basically the same as the answer above; whenever you add a letter to the scene, make sure you update the arraylist corresponding to the letter, and whenever you want to remove one, just find that arraylist and delete its first element.

function AddLetter(l) {
   // do something to create a game object
   letters[l].Add(object);
}

function RemoveLetter(l) {
  if (letters[l].Length == 0) return;
  object = letters[l][0];  // you might need to do some typecasting for C#, like "object = (GameObject) letters[l][0]"
  letters[l].RemoveAt(0);
  Destroy(object);
}