I'm trying to make a simple button in unity using a cube. My plan is for when the cube is highlighted that it will move towards the camera and be highlighted in a specific color and then when it is clicked it will move back and change to a different color. I would also like to know how to make events happen when clicking an object.

  • \$\begingroup\$ Does it necessarily need to be a cube? The reason I ask is that Unity recently introduced a very powerful UI system that makes these types of highlight/click/event-firing interactions super easy, but by default it works primarily with 2D assets (though those 2D planes can be positioned & rotated arbitrarily in 3D space). So the answer will be a little more complicated if you need it to work with a cube model specifically, versus just getting an animated button. \$\endgroup\$ – DMGregory Feb 9 '15 at 22:35

Unity provides a number of features you can use here.

  • OnMouseOver() would be used when you want that highlight effect. This method will be called every frame that the mouse is over your object. Inside this method you'd change the color of your cube and move it however you like (perhaps using some simple harmonic motion to have it bob closer/farther from the camera).

  • OnMouseExit() is used to detect when the object no longer has the mouse over it. You'll need this to re-position an object that is no longer selected.

  • OnMouseDown() is activated when an object is clicked. This would be used to trigger an event when your object is clicked by the user.

These methods require that the object have a Collider of some kind attached to it (the default Cube in Unity already has a Box Collider attached).

As an example, here is a simple script that moves an object towards the camera and slowly rotates it while the mouse is hovered over it. Then prints a debug message when the object is clicked.

public class NewBehaviourScript : MonoBehaviour {

    public Vector3 defaultPosition;
    public Quaternion defaultRotation;

    public float moveTowardsCameraDistance = 3;

    public void Start() {
        defaultPosition = this.transform.position;
        defaultRotation = this.transform.rotation;

    public void OnMouseOver() {
        Vector3 headingToObject = this.transform.position - Camera.main.transform.position;
        this.transform.position = defaultPosition - (headingToObject.normalized * moveTowardsCameraDistance);
        this.transform.Rotate(Vector3.up, 45 * Time.deltaTime, Space.Self);

    public void OnMouseExit() {
        this.transform.position = defaultPosition;
        this.transform.rotation = defaultRotation;

    public void OnMouseDown() {
        Debug.Log(this.name + " was clicked!");

* Most of this code was taken from my course on Unity 3D, you can have 10 days to check it out.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.