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 golbit · Feb 20, 2013 at 01:42 PM · c#animationsoundplayoneshot

Trigger diferent sounds playing from the same object

Hello! My objective is to have 2 buttons, each button makes one object (cube) play a different animation and at the same time it plays a sound for each animation.

For now I have a cube with animations and two Audio Sources in the cube (called 01 and 02) I´m trying to assign each sound to a variable but I don´t know how to do it. If I write 01 it´s a int, "01" string, how to say it´s an AudioSource?

 using UnityEngine;
 using System.Collections;
 
 public class buttons : MonoBehaviour {
 
     void OnGUI () {
         // Make a background box
         GUI.Box(new Rect(10,10,100,150), "Animations");
         
         // sounds
         AudioClip sound1 = 01; // <- i´m guessing my problem is in these lines...
         AudioClip sound2 = 02;
         
         
         // Make the first button
         if(GUI.Button(new Rect(20,40,80,20), "Animation 1")) {
             animation.Play("take_01");
             //audio.Play();
             audio.PlayOneShot(sound1, 1.0F);
         }
 
         // Make the second button.
         if(GUI.Button(new Rect(20,70,80,20), "Animation 2")) {
             animation.Play("take_02");
             audio.PlayOneShot(sound2, 1.0F);
             
         }
                     
     }
 }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by golbit · Feb 20, 2013 at 05:11 PM

Ok, I solved it :)

Instead of working directly with the object I created a prefab and dragged the script there. The public var let me drag the sounds to it and it became very easy to make it working.

 using UnityEngine;
 using System.Collections;
 
 public class buttons : MonoBehaviour {
     
     public AudioClip[] effects;
     
     void OnGUI () {
         // Make a background box
         GUI.Box(new Rect(10,10,100,150), "Animations");
         
         // Make the first button. If it is pressed, Application.Loadlevel (1) will be executed
         if(GUI.Button(new Rect(20,40,80,20), "Animation 1")) {
             animation.Play("take_01");
             audio.PlayOneShot(effects[0], 1.0F);
         }
 
         // Make the second button.
         if(GUI.Button(new Rect(20,70,80,20), "Animation 2")) {
             animation.Play("take_02");
         audio.PlayOneShot(effects[1], 1.0F);
             
         }
         // Make the third button.
         if(GUI.Button(new Rect(20,100,80,20), "Animation 3")) {
             animation.Play("take_03");
             audio.PlayOneShot(effects[2], 1.0F);
         }
         // Make the fourth button.
         if(GUI.Button(new Rect(20,130,80,20), "Animation 4")) {
             animation.Play("take_04");
             audio.PlayOneShot(effects[3], 1.0F);
         }
     }
 }
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

9 People are following this question.

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

Related Questions

Audio Play Once 2 Answers

why not run animation when doublication gameObject ???? 0 Answers

Audio Scripting 1 Answer

Animation Scripting Help #C 1 Answer

Play sound on animation event? 3 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