Awry Array

Hey all, just a general question.

I have a script that gets some buttons in my scene and these buttons are called 1,2,3 and so on up two 25.

My script successfully stores these in an array but not from 1 to 25, instead its a random mish mash of number but it is always the same.

How (if I can) make it correctly store them in ascending order.

This is my script so far.

var currentLevel : int;
var icons : GameObject[];

function Start () 
{
	currentLevel = PlayerPrefs.GetInt("LevelPlaying");
	print(currentLevel);
	icons = GameObject.FindGameObjectsWithTag("Icon");
}

function Update () {

}

When returning the array from the method FindGameObjectsWithTag(), there is no guarantee on how the order will be inside the array. It can be showing up the same every time you use the method in the editor, but could be also completely different when you deploy your game. Unity tracks this internally, which is why the order is seemingly returned the same, and it’s not ‘random’ in a sense every time.

Most likely, you’ll have to sort the array yourself using the System.Array.Sort method:

There are plenty examples online how to numerically and alphabetically order your array using an IComparer.