- Home /
Play sound on click GUIbutton
So far, for a school project, I have been able to get a GUI button to display upon detection of an imagetarget. What I haven't been able to figure out however is how to get sound to play when this button is clicked even though I get no error. The code for the popup GUI button can be found below.
I think the error is somewhere under the line "// do something on button click".
`
using UnityEngine;
using System.Collections;
using Vuforia;
public class ButtonPopup : MonoBehaviour, ITrackableEventHandler
{
public AudioClip Gymnopedie;
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
this.gameObject.AddComponent<AudioSource>();
this.GetComponent<AudioSource>().clip = Gymnopedie;
this.GetComponent<AudioSource>().Play();
}
}
}
}
Help would be greatly appreciated! :)
Comment