ui button input

as easy as this is and i know its going to be easy i can not find anything about this ive searched for hours and cant find anything that helps my situation.

i have created a UI button ( not from script) and i need to get it to change a bool value in a script.

i have the script added to this button but cant figure out how to get the click input for this button.

in new to unity and still have very novice skills.

Note, if you don’t already have one, you will need a collider on your button if you want this to work. Also, assuming you’re refering to mouse left click.

using UnityEngine;
using System.Collections;

public class buttonTest : MonoBehaviour {

    void Update () {

        if(Input.GetMouseButtonDown (0)){

            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast (ray, out hit, Mathf.Infinity)) {
                if(hit.transform == transform) {
                    accessYourBoolHere;
                }
            }
        }
    }
}

I think you are using the new Unity UI Button.

If thats the case then :

  1. Make a script with a public method where you change the bool value.
  2. Put this script on some gameobject on the scene.
  3. Select the UI button and then click on the + in UI buttons OnClick in the Inspector
  4. Drag n Drop the gameobject created in step 2 into the UI buttons OnClick in Inspector.
  5. Select the public method that you created in step 1 from the dropdown.

You may want to check this :

http://unity3d.com/learn/tutorials/modules/beginner/ui/ui-button

1 Like