Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

A bit of help solving a Button problem

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

  1. DanielWG

    DanielWG

    Joined:
    Nov 16, 2021
    Posts:
    2
    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;
    4.  
    5. public class PlayAudio : MonoBehaviour {
    6.     public void OnPlay () {
    7.  
    8.         GetComponent<AudioSource> () .pitch = 1f;
    9.         GetComponent<AudioSource> () .Play ();
    10.  
    11. }
    12. }
    13.  
    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!