Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Xain · Sep 23, 2014 at 04:33 PM · buttonaudio

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))
         {
 
         }
         
         }
 
     }    
 
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image N1warhead · Sep 23, 2014 at 05:00 PM 1
Share

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.

avatar image Xain · Sep 23, 2014 at 05:36 PM 0
Share

Thanks for answer it worked wery well :) , is this code works on javascript too or ?

avatar image N1warhead · Sep 23, 2014 at 09:09 PM 1
Share

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?

avatar image Xain · Sep 24, 2014 at 04:09 PM 0
Share

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 .

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

27 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges