Holding down UI mobile button

Hello all,

I am total beginner with unity. I am developing 3d car simulation game playable on mobile devices(android) and I dont know how to make break lights via UI button. I want to do when user hold the break button some spot light will be enabled. Is it possible to make?
Thanks for any help:)

I had a similar set up as what you’re going for, so just try this. Add the Event Trigger component to your button, and then add the two events as shown below.


You’ll want to make a new script A, and have it say:

public class A : Monobehaviour
    public GameObject breaklights; //Drag your breaklight here

    public void onPress ()

    public void onRelease ()

Then, for B, you’ll select onPress(), and for C, you select onRelease(). This will call onPress once when the button is initially pushed, and onRelease once when the button is initially released.

Sure thing.

You see, touches get handled just like a mouse most of the time.

So just add some like this to you button:

public class MyFreshButton : MonoBehaviour
	public Light myLight;

	void Update ()
		if (Input.GetMouseButtonUp (0) && myLight.enabled)
			myLight.enabled = false;

	void OnMouseDown ()
		myLight.enabled = true;

This will activate the light when you click the button and deactivate it when you lift your finger (regardless of if its on the button or not).

Of course your button needs a collider and if its a child of an object that has a rigidbody it also needs its own one.

Good luck :smiley:


thanks for your answer but is still isnt working correctly. I aded your script to my button but it only works that when I set in editor light to true and in game touch the button it set it to off position. And then nothing. If I set light to off in editor it does nothing from start. Any idea?:slight_smile:

@Madmaxx9000 do those and drag in your spot light in the mylight thing on the object where you putted the script on.