Random Cube Replacement - Swap

Hello to all.

My question is, how to make three cubes randomly change on three STATIC locations. Or three different shapes, to change only on three static places. I have it made the textures to change but that’s not what I need. For example, three cubes with different colors swap on three defined positions. Thank you.

Cheers.

So you just want to change the position of the cubes? Well, you could have a array that contains the coordinates of those 3 places or just a array of empty game objects that are placed where you want.

Something like…

var ar = new Array();
ar[0] = emptyGameObject_1;
ar[0] = emptyGameObject_2;
ar[0] = emptyGameObject_3;

an whenever you want to change the position of a cube just do something like…

cube.transform.position = ar[0].transform.position;

or something along those lines… I’m not too sure if the code would work because I’m still new to this,but I hope that you get the idea.

Sorry for the very late reply. I tried it somehow modifying it but didn’t work.

I realize that it should be in an array, just like you said, but it also must be so that the swapped locations are always different. What if two or three cubes go to one same location? That’s what I wanted to ask I got lost there.

Thanks.

I hope to have understand
Try with this:

public GameObject[] cubes;
public Vector3[] pos;
public bool swap;

void Start()
{
  cubes[0] = pos[0];
  cubes[1] = pos[1];
  cubes[2] = pos[2];
}

void Update()
{
    if(swap)
    {
          swap = false;
          int randomPos1 = Random.Range(0,2);
          int randomPos2 = Random.Range(0,2);
          if(randomPos1 == randomPos2)
          {
                     swap = true;
                     return;
          }
          SwapCubes(randomPos1,randomPos2);
    }
}

void SwapCubes(int toSwap1,int toSwap2)
{
        _cubes[toSwap1].position = pos[toSwap2];
        _cubes[toSwap2].position = pos[toSwap1];
}

It seems a bit confusing but I’ve made it in the forum because I don’t have unity now on this pc, try…

Hi and thanks for the reply.

Tried it and it gives me several same errors: - error CS0116: A namespace can only contain types and namespace declarations - it gives this error to these lines

public GameObject[] cubes;
public Vector3[] pos;
public bool swap;

and also a Parsing error on the end of void SwapCubes.

Any chance of remaking it? Thanks.

Hey, how do you copy the script? Because it needs to import the namespaces and to be inserted in the class, if you can wait when I go to home I can make it in unity so I can give you all the script…

I am surely doing something wrong, so I’ll wait for you if you are able to make it. Thanks.