Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

A bit of help solving a Button problem

Discussion in 'UGUI & TextMesh Pro' started by DanielWG, Nov 16, 2021.

  1. DanielWG


    Nov 16, 2021
    Hello everyone!

    So, basically, I'm doing a very simple app for a music project, it basically is a "Little piano" type app, in which you press different buttons to generate sounds and can play around alternating them.

    It's all quite functional for now, but I have a little problem with the buttons, you see, I used simple preset buttons that Unity allows me to just throw around, and set them up to activate a "Hold" On Click, which is then set up with this code right here (all things I got from YT tutorials, my coding knowledge is near inexistent)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    5. public class PlayAudio : MonoBehaviour {
    6.     public void OnPlay () {
    8.         GetComponent<AudioSource> () .pitch = 1f;
    9.         GetComponent<AudioSource> () .Play ();
    11. }
    12. }
    The problem then is very simple, this whole set up does it so that, when you press the button with your finger (or the initial mouse click) it won't activate until you let go of it, and I want it to trigger when you press it initially, not when letting go.
    But I of course didn't created my own button script, and just used the Unity default, so I don't know how to change this and would like some help.

    Thanks for your attention!