how to exchange the index of an object in an array

Hi
I’d like to know how can I swap/exchange the index of objects of an array.
For example:
1.Imagine I have 3 objects in an array (0.Red, 1.Blue and 2.Green).
2.After Pressing the space key, I’d the Red to go to where Blue is, the Blue to go to where Green is and the Green to go to where the Red is.
3.I don’t what each object to only move to the position of another object, but I want them to change their index to the index of the target object as well.
Now so far I’ve figured how to make the re-positioning, but I have no clue about how to change the value of index as well.
I’ve used a trick to get what I want, I’m using secondary(temporary) vars to tag the objects based on their position but that’s no solution at all since it;s strictly limited to the situation.

using UnityEngine;
using System.Collections;

public class movement : MonoBehaviour {
    public GameObject[] objects1;
    public static int movespeed = 1000;
    public GameObject pivot;
    public GameObject first;
    public GameObject second;

    // Use this for initialization
    void Start () {
        objects1[0] = GameObject.Find("Red");
        objects1[1] = GameObject.Find("Blue");
        objects1[2] = GameObject.Find("Green");
        objects1[0].transform.position = new Vector3(0, 0, 0);
        objects1[1].transform.position = new Vector3(-2, 0, 0);
        objects1[2].transform.position = new Vector3(-1, 2, 0);
        pivot = objects1[0];
        first = objects1[1];
        second = objects1[2];
    }
	
	// Update is called once per frame
	void Update () {
        
        if (Input.GetKeyDown("space"))
        {
            var pos0 = pivot.transform.position;
            var pos1 = first.transform.position;
            var pos2 = second.transform.position;
            pivot.transform.position = Vector3.MoveTowards(transform.position, pos1, movespeed * Time.deltaTime);
            first.transform.position = Vector3.MoveTowards(transform.position, pos2, movespeed * Time.deltaTime);
            second.transform.position = Vector3.MoveTowards(transform.position, pos0, movespeed * Time.deltaTime);

            //useless trick:

            if (objects1[0].transform.position == new Vector3(0, 0, 0))
            {
                pivot = objects1[0];
            }
            else if (objects1[1].transform.position == new Vector3(0, 0, 0))
            {
                pivot = objects1[1];
            }
            else if (objects1[2].transform.position == new Vector3(0, 0, 0))
            {
                pivot = objects1[2];
            }
            if (objects1[0].transform.position == new Vector3(-2, 0, 0))
            {
                first = objects1[0];
            }
            else if (objects1[1].transform.position == new Vector3(-2, 0, 0))
            {
                first = objects1[1];
            }
            else if (objects1[2].transform.position == new Vector3(-2, 0, 0))
            {
                first = objects1[2];
            }
            if (objects1[0].transform.position == new Vector3(-1, 2, 0))
            {
                second = objects1[0];
            }
            else if (objects1[1].transform.position == new Vector3(-1, 2, 0))
            {
                second = objects1[1];
            }
            else if (objects1[2].transform.position == new Vector3(-1, 2, 0))
            {
                second = objects1[2];
            }
            Debug.Log("The pivot is: " + pivot);


        }
        
    }

}
  1. Find the index of element1 to move in.
  2. Find the index of element2 to evict and move.
  3. Store the contents of element2 in a temporary variable.
  4. Set the value of element2 to the value of element1.
  5. Set the value of element1 to the temporary variable.

That’s a simple way, which I recommend you try to do first to understanding how it works. To get it so all the elements are being moved around, like a queue. I.e. the first element gets pushed to the back, therefore its constantly rotating, use a for loop.

You can use the above pseudocode to help you, but you will need to deal with the first element being move to the last element. Use a flow diagram to help you plan the flow of your algorithm before attempting the code. It doesn’t have to be anything detailed, but should show the flow of information which anyone could follow and understand what it does on paper.

Give it a try, have a think about it logically, try different things. If you are struggling show us your best attempt at what you have come up with if you reply.

Thanks a lot guys, I managed to get it running. now I have 2 different cycles :slight_smile: but funny thing is that it turned to be anti-clockwise :smiley:

![alt text][1]

Here is the code in case someone has a similar question:

using UnityEngine;
using System.Collections;

public class movement : MonoBehaviour {
    public GameObject[] objects1;
    public static int movespeed = 1000;
    public GameObject tmp1;

    // Use this for initialization
    void Start () {
        objects1[0] = GameObject.Find("Red");
        objects1[1] = GameObject.Find("Blue");
        objects1[2] = GameObject.Find("Green");
        objects1[3] = GameObject.Find("Orange");
        objects1[4] = GameObject.Find("Yellow");
        objects1[0].transform.position = new Vector3(0, 0, 0);
        objects1[1].transform.position = new Vector3(-2, 0, 0);
        objects1[2].transform.position = new Vector3(-1, 2, 0);
        objects1[3].transform.position = new Vector3(2, 0, 0);
        objects1[4].transform.position = new Vector3(1, -2, 0);

    }
	
	// Update is called once per frame
	void Update () {

        if (Input.GetKeyDown(KeyCode.A)) {
            for (int i= 0; i< objects1.Length; i++)
        {
            Debug.Log( i + "is: " + objects1*);*

}
}

if (Input.GetKeyDown(“space”))
{
var pos0 = objects1[0].transform.position;
var pos1 = objects1[1].transform.position;
var pos2 = objects1[2].transform.position;
objects1[0].transform.position = Vector3.MoveTowards(transform.position, pos2, movespeed * Time.deltaTime);
objects1[1].transform.position = Vector3.MoveTowards(transform.position, pos0, movespeed * Time.deltaTime);
objects1[2].transform.position = Vector3.MoveTowards(transform.position, pos1, movespeed * Time.deltaTime);
tmp1 = objects1[0];
objects1[0] = objects1[1];
objects1[1] = objects1[2];
objects1[2] = tmp1;
}
if (Input.GetKeyDown(KeyCode.S)) {
var pos0 = objects1[0].transform.position;
var pos3 = objects1[3].transform.position;
var pos4 = objects1[4].transform.position;
objects1[0].transform.position = Vector3.MoveTowards(transform.position, pos4, movespeed * Time.deltaTime);
objects1[3].transform.position = Vector3.MoveTowards(transform.position, pos0, movespeed * Time.deltaTime);
objects1[4].transform.position = Vector3.MoveTowards(transform.position, pos3, movespeed * Time.deltaTime);
tmp1 = objects1[0];
objects1[0] = objects1[3];
objects1[3] = objects1[4];
objects1[4] = tmp1;
}
}

}

[1]:http://i.imgur.com/2Fh1eyR.gif