Puzzle game: Making one object switch places with another

234 Views Asked by At

I'm VERY new at programming in unity and it really is not my forte. But I really need help with this.

I need help making a code that will change places with two game objects (8 in total all need to be able to switch with eachother) I want to use the mouse left key click to work.

All I got this far is:

void OnMouseDown () { transform.position = otherObject.transform.position; }

but this doesn't do anything.. please help! :)

1

There are 1 best solutions below

0
On

For OnMouseDown() message to work you have to attach a Collider component. Go to Component->Physics menu and select a collider that fits your object. Most likely you want to use a Box Collider.

To switch objects you can do this:

void OnMouseDown ()
{
    Vector3 temp = transform.position;
    transform.position = otherObject.transform.position;
    otherObject.transform.position = temp;
}