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 El-Deiablo · May 02, 2016 at 05:15 AM · arrayaudio2d gametriggersounds

Can't figure out how to play a random audio clip when player collides with enemy

I thought this would have been the easiest part of making a game! I have been researching and trying different codes for days and have not come anywhere close to figuring this out. I have an array of 6 sounds. I want 1 of the 6 sounds to randomly play when the player collides with an enemy. I know how to do this part, but the audio just won't play. All the videos and articles I read are about setting an audio source on the object and playing "one" sound when a trigger occurs. My enemies are prefabs that spawn and destroy themselves after some time. I also have a particle system that triggers when the enemy is destroyed. Why does the particle system work, but the sound won't? If I even try to comment out everything in the if statements (in the ontriggerenter2d method) and leave only the audio file, no sounds are heard. There will be lots of collisions in my game and I want to add a variety of sounds. This is driving me crazy!

Here is my current code:

 using UnityEngine;
 using System.Collections;
 using UnityEngine .Audio ;
 
 public class FlipHead : MonoBehaviour {
 
     public float speed;
     private SpriteRenderer mySpriteRenderer;
     public GameObject particle;
     public GameObject particle2;
     public AudioClip[] clips;
     public AudioMixerGroup output;
 
 
 
     public AudioSource noise;
 
     //private float someScale;
     //private float direction, _posX;
 
     void Awake () 
     {
         mySpriteRenderer = GetComponent<SpriteRenderer>();
         //direction = 1;
         //someScale = transform.localScale.x;
     }
 
     void Start(){
     }
 
     void Update()
     {
 
         if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Moved) {
             Vector2 touchDeltaPosition = Input.GetTouch (0).deltaPosition;
             transform.Translate (touchDeltaPosition.x * speed, touchDeltaPosition.y * speed, 0);
 
         }
 
         if(mySpriteRenderer != null)
         {
             if(Input.GetTouch (0).deltaPosition .x<0)
             {
                 // flip the sprite
                 mySpriteRenderer.flipX = true;
             }
 
             if(Input.GetTouch (0).deltaPosition.x >0)
             {
                 // flip the sprite
                 mySpriteRenderer.flipX = false;
             }
 
     }
 }
 
     void OnTriggerEnter2D(Collider2D  other){
         
         if (other.tag == "Running Player") {
             GameObject clonedParticle;
             GameObject sound;
             clonedParticle =Instantiate (particle, transform.position, transform.rotation) as GameObject ;
             int randomClip = Random.Range (0, clips.Length);
             sound=Instantiate (clips[randomClip ], transform.position, transform.rotation)as GameObject ; 
             //noise.PlayOneShot (clips [randomClip]);
             Destroy (other.gameObject); 
             Destroy (sound, clips [randomClip].length);
             Destroy (clonedParticle, 2);
 
 
 
             }
         if (other.tag == "Running Ref") {
             //int soundIndex = Random.Range (0, BiteSounds .Length);
 
             GameObject clonedParticle2;
             clonedParticle2 =Instantiate (particle2, transform.position, transform.rotation) as GameObject ;
             Destroy (other.gameObject);
             Destroy (clonedParticle2,2);
         }
         }
 
     /*void PlaySound(){
 
         int randomClip = Random.Range (0, clips.Length);
         AudioSource source = gameObject.AddComponent <AudioSource > ();
         source.clip = clips [randomClip];
         source.outputAudioMixerGroup = output;
         source.Play();
         Destroy (source,clips [randomClip ].length );
 
     }*/
     
     }
     



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
Best Answer

Answer by Cherno · May 02, 2016 at 11:02 AM

 if (other.tag == "Running Player") {
              GameObject sound = new GameObject();
              sound.transform.position = transform.position;
              int randomClip = Random.Range (0, clips.Length);
              AudioSource audioSource = sound.AddComponent<AudioSource>();
              audioSource.clip = clips[randomClip ];
              audioSource.Play();
             Destroy (sound, clips [randomClip].length);
              }
Comment
Add comment · Show 1 · 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 El-Deiablo · May 03, 2016 at 02:44 AM 0
Share

You are amazing sir! This works great!! Thank you so much!!!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Audio Trigger 2D in C#? 1 Answer

Trigger detects player collision and play specific audio from array 1 Answer

FPS Audio Issue 1 Answer

IndexOutOfRangeException error 1 Answer

Help detecting texture once inside trigger object 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