Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Ramus1973 · May 31, 2018 at 05:30 AM · audioaudiosourceaudioclipaudio.playoneshot

Playing multiple audio clips

I am trying to play 2 samples in 1 script, 1 for when enemy gets hit and 1 for when it dies. But for the life of me I cannot get more than 1 sample to play. I have tried about 5 or 6 methods from googling the issue and none of them work. I really don't see how it is so difficult to play more than 1 sound effect. Have tried the AudioSource[] AudioClips = null; and using AudioClips[0].Play(); but that still didn't play the second audio clip. The method I am using now doesn't work. Can someone give me a way to play 2 samples that works? Will add all the code in my script just for clarity and an image of how the inspector is set up.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 [RequireComponent(typeof(AudioSource))]
 public class Enemies : MonoBehaviour {
 public float movespeed = 5;
 public int starthealth = 50;
 private int curhealth;
 public Transform explode1;
 private float x;
 private float y;
 private float z;
 public AudioClip Pig1;
 public AudioClip Pig2;
 public AudioSource source1;
 public AudioSource source2;
 void OnCollisionEnter(Collision col)
 {
     if(col.gameObject.tag == "Prefab")
     {
         Destroy(col.gameObject);
         curhealth = curhealth - 10;
         if(curhealth > 9)
             source1.PlayOneShot(Pig1,1f);
     }
     if(curhealth <= 0)
         {
         source2.PlayOneShot(Pig2,1f);
         x = gameObject.transform.position.x;
         y = gameObject.transform.position.y;
         z = gameObject.transform.position.z;
         Instantiate(explode1, new Vector3(x, y, z), Quaternion.identity);
         Destroy(gameObject);
         }
 }
     void Awake ()
     {
         source1 = GetComponent<AudioSource>();
         source2 = GetComponent<AudioSource>();
         curhealth = starthealth;
     }
     void Update () {
         transform.Translate(Vector3.forward * movespeed * Time.deltaTime);
     }
 }
 

alt text

wtf.jpg (52.6 kB)
Comment
Add comment · Show 1
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 Ramus1973 · Jun 01, 2018 at 02:26 AM 0
Share

The FirstPersonController script from the asset has multiple samples playing so I copied how they used play audio clip in their script and matched inspector settings. AND IT STILL DOESN'T WOR$$anonymous$$!!! How the bloody hell do you play more than 1 sample? It's getting ridiculous now.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Positive7 · Jun 01, 2018 at 02:34 AM

 source1 = GetComponents<AudioSource>()[0];
 source2 = GetComponents<AudioSource>()[1];
Comment
Add comment · Show 3 · 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
avatar image Ramus1973 · Jun 01, 2018 at 06:24 PM 0
Share

Cannot apply indexing with [] to an expression of type `UnityEngine.AudioSource'

avatar image Positive7 Ramus1973 · Jun 01, 2018 at 09:29 PM 0
Share

GetComponents not GetComponent

avatar image Ramus1973 Positive7 · Jun 02, 2018 at 04:22 AM 0
Share

Thanks I found that out (plurals) after 30 $$anonymous$$utes of googling lol. Unfortunately using that method still only 1 of the samples would play.

avatar image
0

Answer by Ramus1973 · Jun 02, 2018 at 04:14 AM

I have kind of found a solution but it is far from ideal. For some reason trying to use an audio source from the gameObject means only 1 sample will play. I created an empty object and attached an audio source to that and gave it the tag Audio. Then used _audio = GameObject.FindGameObjectWithTag("Audio"); and I can use 2 samples??? I just don't understand why I can't play 2 samples using the audio source from the gameobject itself? I'm assuming _audio = gameObject would get the audio source from the object the script is attached to? But doing that only 1 sample plays....... Completely confused by something as simple as playing 2 samples.

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

163 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 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 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 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 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 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 avatar image

Related Questions

Why won't my second Audio Source play? 0 Answers

oneshot not working 0 Answers

Stop Audio Source Clip while button Press 1 Answer

can i make a loop for each with all audiosources of my project ? 0 Answers

Second AudioClip won't play 0 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