Z-Index | Organise Int Array - Sequence

Hello,

I can’t get my head around on how to do this. I have a List that stores the value of a Z-Index of my dynamic objects, which are from 0 to Infinity.

0 being the bottom. Infinity being the top. It’s infinity because the user can ‘Instantiate’ any number of these objects. For example, the user has initiated 5 objects, the List would look something like this:

  • Object A = 0 [Bottom]
  • Object B = 1
  • Object C = 2
  • Object D = 3
  • Object E = 4 [Top]

The user has the ability to click/tap on any of these objects. With this, I would like the Z-index list to reorganise itself so the most recent tapped object is on the highest. For example, user taps Object D, List needs to look like this:

  • Object A = 0 [Bottom]
  • Object B = 1
  • Object C = 2
  • Object D = 4 [Top]
  • Object E = 3

Then taps on Object A:

  • Object A = 4 [Top]
  • Object B = 0 [Bottom]
  • Object C = 1
  • Object D = 3
  • Object E = 2

I just can’t get my head around on how to even implement this…? Also note - the Z-Index has nothing to do with the actual transform.position.z. It’s just a sequence of int numbers from 0 to the number of Initiated objects.

var objectsList : List.<GameObject>;
var objectsDepth : List.<int>;

//this is called when the user is initiating a new object
function UserInitatedObject(){

	var obj = Instantiate(object);
	objectsList.Add(obj);

	//make the newest object the highest depth value
	objectsDepth.Add(objectsDepth.Count);

}


//this is called when user has tapped on object within scene
function UserTappedOnObject(index : int, object : GameObject){

	//need to reorganise 'objectsDepth' list so that this object is now the highest
}

That was actually a lot easier than I thought:

//this is called when user has tapped on object within scene
function UserTappedOnObject(index : int, object : GameObject){

	//need to reorganise 'objectsDepth' list so that this object is now the highest
	var thisDepth = objectsDepth[index];

	for(i = 0; i < objectsDepth.Count; i++){

		if(objectsDepth *> thisDepth){*

objectsDepth = objectsDepth -1;
* }*
* }*

* //make this the highest*
* objectsDepth[index] = objectsDepth.Count-1;*
}