Play sound when clicking GUI button
For a school project I made an AR app which can succesfully recognize target images and display 3D models on them. The next step would be to create a button which would pop up upon recognizing the target image and which would play a sound when the user clicks on it. Using the script below I have been able to make a button pop up but I haven't been able to get a sound to play when the user clicks the button.
using UnityEngine;
using System.Collections;
using Vuforia;
public class ButtonPopup : MonoBehaviour, ITrackableEventHandler
{
public GameObject Button;
public Texture btntexture;
public Texture LogoTexture;
public Texture MobiliyaTexture;
private TrackableBehaviour mTrackableBehaviour;
private bool mShowGUIButton = false;
private Rect mButtonRect = new Rect(50, 50, 120, 60);
void Start()
{
mTrackableBehaviour = GetComponent<TrackableBehaviour>();
if (mTrackableBehaviour)
{
mTrackableBehaviour.RegisterTrackableEventHandler(this);
}
}
public void OnTrackableStateChanged(
TrackableBehaviour.Status previousStatus,
TrackableBehaviour.Status newStatus)
{
if (newStatus == TrackableBehaviour.Status.DETECTED ||
newStatus == TrackableBehaviour.Status.TRACKED)
{
mShowGUIButton = true;
}
else
{
mShowGUIButton = false;
}
}
void OnGUI()
{
if (mShowGUIButton)
{
// draw the GUI button
if (GUI.Button(mButtonRect, "Turtle"))
{
// do something on button click
}
}
}
}
I get that I should add a function which would play the sound after the line which reads "// do something on button click " but I haven't been able to find this command.
It would be greatly appreciated if someone could explain to me what the function is which I should use in this place. Thanks a lot in advance!! :)
Your answer
![](https://koobas.hobune.stream/wayback/20220612124617im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Open URL when touch object android 0 Answers
Vuforia model targeting AR: help wanted 1 Answer
Let Camera follow (rotate towards) object only til' a certain degree? 0 Answers
Transform.LookAt when enter trigger 0 Answers
Hold to jump higher 0 Answers