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!