Play Audio on Object click?

So earlier, I asked a question regarding creating a GUI on the click of an object. And I was helped out by @ahmedbenlakhdhar, @static_cast, and @Titirez5. I recently realized that I probably don’t need the GUI though :confused:
I realized that I might need an audioclip. So, I tried to figure out how exactly it works, but I clearly was very confused.

So, here’s my script. I am hoping to have it so that, rather than a texture, I get a variable for an AudioClip, and the there should be something about an AudioSource…
Anyway, here is what I have as of now:

using UnityEngine;
using System.Collections;

public class Click : MonoBehaviour {

public Texture tex;
public GameObject MYOBJECT = null;
//public float GUItrialone = GUI.Button(350, 350, (10, 20 "Try!"))
bool        displayGUI = false;
void    OnGUI()
{
	if (!tex)
		Debug.LogError ("Really Dude? After all this, you forget a TEXTURE???");

	if (Input.GetMouseButtonUp(0))
	{
		Ray            ray = Camera.main.ViewportPointToRay(Camera.main.ScreenToViewportPoint(Input.mousePosition));
		RaycastHit    outinfo;
		
		if (Physics.Raycast(ray, out outinfo, Mathf.Infinity))
		{
			//TOUCH! Show GUI
			if (Physics.Raycast(ray, out outinfo, Mathf.Infinity))
			{
				if(outinfo.transform.gameObject == MYOBJECT){
					displayGUI = true;
				}
			}

		}
	}
	if (displayGUI == true)
	{
		if(GUILayout.Button(tex))
			displayGUI = false;
		//if (GUILayout.Button (tex)) 
		//{
			//displayGUI = false;
		//}
	}

}

}

Any help is greatly appreciated!
Thank you!

EDIT:

So, here is a more updated script. My problem now is that I can’t assign an AudioSource. Whenever I click to assign an AudioSource, and I look at either the Assets or the Scene tabs, it says there’s none. Here it is:

using UnityEngine;
using System.Collections;

public class ClickAudio : MonoBehaviour {

public AudioSource Source;
public AudioClip Clip;
public GameObject MYOBJECT = null;
bool        displayGUI = false;
void    OnGUI()
{
	

	
	if (Input.GetMouseButtonUp(0))
	{
		Ray            ray = Camera.main.ViewportPointToRay(Camera.main.ScreenToViewportPoint(Input.mousePosition));
		RaycastHit    outinfo;
		
		if (Physics.Raycast(ray, out outinfo, Mathf.Infinity))
		{
			//TOUCH! Show GUI
			if (Physics.Raycast(ray, out outinfo, Mathf.Infinity))
			{
				if(outinfo.transform.gameObject == MYOBJECT){
					displayGUI = true;
				}
			}
			
		}
	}
	if (displayGUI == true)
	{

		Source.Play();
		
	}
	
	
}

}

Again, any help is greatly appreciated!

Audio is componentised.

It means you need to add the component to an object you want to play audio. From here you add (and assign) audio clips. Once the clip is assigned you can play it.

Load your clips in to AudioClip, then use the Play function :slight_smile:

The source determines where the sound comes from. For example if you put the source next to the listener, this is good for Game Music. You can have multiple sources, but one Listener.

You won’t need GUI if you use RayCast to ‘touch’ an object. Note that a collider is important in this case.

1.make a new script
2.void OnMouseDown() {
//TODO:play sound?
}
3.drag to your object.