- Home /
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 :/ 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!
Oh. Sorry, that was really unclear! $$anonymous$$y question is about how to add in an AudioClip rather than a GUI. But, there's a bunch of stuff like AudioSource and such, which overcomplicates it for me, so I needed help.
Oh yeah. I know I don't need a GUI. I just wanted to know how to add in the Audio stuff to my script.
NONE means there's no audioclips in your project. If you do actually have a clip, find it in the project explorer and drag-n-drop.
Answer by meat5000 · Dec 20, 2014 at 04:01 PM
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 :)
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.
Answer by flapy-monkey · Dec 24, 2014 at 05:52 PM
1.make a new script 2.void OnMouseDown() { //TODO:play sound? } 3.drag to your object.
Your answer
Follow this Question
Related Questions
Problems combining Audio triggers, PlayOneShot and Audio Arrays 1 Answer
How to switch Audiosource without stacking by an input? 0 Answers
2nd Audio Clip Not Playing 0 Answers
Why PlayOneShot is not working? C# 1 Answer
Audio stops playing after releasing my Mouse Button and pressing another key 2 Answers