• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

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

Controlling value of blend parameter via script 0 Answers

Multiple Cars not working 1 Answer

How can I get the legnth of an animation? (Mechanim Animator/C#) 2 Answers

Smooth transition from Camera1 to Camera2? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges