Simulating button press

I’m moving my first steps into Unity and I’m trying to build a simple menu for my game.

I’m using some cubes to simulate the buttons, and I’ve been able to animate them on mouse over.

What I’m not able to do is to animate them on the button press, and I don’t get why I’m failing.

This is my scene’s structure:

  1. GameManager: an empty GameObject with the following script attached (JS)

    private var ray : Ray;
    private var hit : RaycastHit;

    function Start () {
    }

    function Update () {
    MenuItemButton.enabledButton = “”;
    ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    if(Physics.Raycast(ray, hit)){
    if(Input.GetMouseButtonDown(0)){
    MenuItemButton.isClicked = true;
    } else {
    MenuItemButton.isClicked = false;
    }
    MenuItemButton.enabledButton = hit.transform.name;
    }
    }

  2. PlayMenuItem/OptionsMenuItem: 2 cubes to simulate the buttons, with the following script attached (JS)

    static var enabledButton : String;
    static var isClicked : boolean;

    function Start () {
    enabledButton = “”;
    isClicked = false;
    }

    function Update () {
    if(enabledButton.Equals(gameObject.name)){
    if (isClicked) { OnClick(); }
    else {OnSelected(true); }
    } else {
    OnSelected(false);
    }
    }

    function OnSelected(selected:boolean){
    if(selected){
    iTween.MoveTo(gameObject, {“z”:-2, “time”:0.5});
    } else {
    iTween.MoveTo(gameObject, {“z”:0, “time”:0.5});
    }
    }

    function OnClick(){
    iTween.MoveTo(gameObject, {“z”:50, “time”:0.5});
    print("Clicked on " + gameObject.name);
    }

When I’m hovering the button, it moves fine and there are no problems, but when I click on it it just stays still in the “hover” position.

What’s wrong with my code?

Edit: Actually I doubt this is your problem, use print statements so you can see where you are getting because its hard to see what’s going on at a glance, and I would move the raycast in the if clicked because you are wasting cycles checking this every update without a click.

Also, make sure objects are on layers that don’t ignore raycast.

Input.getMouseButtonDown will only be true for a fraction of a second (The first click before release), Unity - Scripting API: Input.GetMouseButtonDown
before next update it comes around and sets it back to false.