Reverse object position order

Hi!
I have a small problem maybe more mathematical than unity, but it’s still the right place I think.
I’m trying to reverse the object location order detect by a raycast (this maybe not be the right solution to use a raycast) in a click.

I explain myself with a drawing :

alt text

I used empty gameobject as a location for my objects, but I do not have the “code” in mind to operate a “conversely” …

Because in drawing, there are only three objects, but I would like it to be a much larger number too.
And with a possibility to click on any object also that suddenly, would operate a reversal with the objects only above him.

Thanks in advance :slight_smile: !

So assuming you have the list of all the objects (either by raycast or other means):

  1. Iterate through half the list
  2. For each element “i” swap it’s place with element “list.Count - i”
  3. To swap place (assuming you want to swap their position) you need a Vector3 temporary variable

Something like this (not tested, a little bit pseudo-code’y):

List<GameObject> objects = getAllObjects
int count = objects.Count;
for (int i = 0; i < Mathf.FloorToInt(count / 2); i++)
{
	Vector3 temp = objects*.position;*

_ objects*.position = objects[count - i - 1].position;_
_
objects[count - i - 1].position = temp;_
_
}*_