- Home /
Audio Play on Touch
Hello everyone , i want to do audio effect when button pressed i tried couple things but couldn't find im using C# , this is my code ;
using UnityEngine;
using System.Collections;
public class Menu : MonoBehaviour {
public GUIStyle buttonStyle ;
public GUIStyle button2;
public AudioClip source1;
void OnGUI ()
{
if(GUI.Button(new Rect(Screen.width/250,Screen.height/10,Screen.width/2,Screen.height/5), "" , buttonStyle))
{
Application.LoadLevel("Level1");
}
if(GUI.Button(new Rect(Screen.width/120,Screen.height/3.9f,Screen.width/2,Screen.height/5), "" , button2))
{
}
}
}
There is plenty of stuff out there if you spend more than 5 $$anonymous$$utes to search for it lol... (Not being rude), but the answer to your question is........ Are you ready??
audio.PlayOneShot(source1);
That will play your Audio file 1 time.
$$anonymous$$ake sure the object your script is attached to has an Audio Source and it will work.
if this dont work then your button is incorrectly made lol.
If this works please mark my answer as correct. Thank you.
Thanks for answer it worked wery well :) , is this code works on javascript too or ?
You're very welcome. It should, but don't want to promise... I started with C#, went to UnityScript and now back to C#, but as far as I can remember YES it will work..
If not, it's very slight differences in coding.
$$anonymous$$ay you please mark the answer I gave you as correct so people searching may know how to fix?
Uhm thanks for answer but you did this as a comment not an answer that's why i don't think i can do that or maybe i don't see "Accept this answer as correct" thing .
Answer by 767_2 · Sep 24, 2014 at 04:18 PM
after adding audio source and audio listener you can simply use this
audio.PlayOneShot(source1);
but if you want to use this for effects that plays often in games you should make a pool manager for your sounds , check this links
soundMnager cs script
http://www.blog.silentkraken.com/2010/04/06/audiomanager/
http://u3d.as/content/dark-tonic-inc-/master-audio-aaa-sound-solution/3PY
Your answer
Follow this Question
Related Questions
Can i fire off a function if ANY button is pressed? 1 Answer
Button to mute and un mute audio not working. 0 Answers
Mute Unity Ads in code? 2 Answers
Play sound on click GUIbutton 0 Answers
Play Sound when GUI button is pressed. 7 Answers