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 Mimk · May 13, 2015 at 10:58 AM · arrayaudiorandomaudioclipaudioclips array

Need help to play a random explosion sound from an array.

I am an aspiring Game Sound Designer, I have worked on film for some years and I want to make the jump.

So, I did the Space Shooter tutorial, but now I am trying to implement my own sound design to it, I already manage to add my own laser sounds using an array, so it plays variations of a laser shot to make it more dynamic and interesting.

Now I am trying to do the same with the asteroid's explosions, but when I add the different sounds to the asteroid explosion prefab and I leave the Audio Source setting like on the tutorial, they are set to play on awake when the asteroid explosion Prefab is instantiated , obviously, all the sounds play same time.

So, the question is, how do I make an array to play random sounds when the asteroid explosion is instantiated? I created a script (C#) and added it to the asteroid explosion prefab, I also assigned the audio clips on the array in the inspector.

here it is:

 using UnityEngine;
 using System.Collections;
 
 public class AsteroidBang : MonoBehaviour 
 {
     public AudioClip[] asteroidbang;
 
     private AudioSource bang;
 
 
     void Awake()
     {
         bang = GetComponent<AudioSource> ();
     }
 
     void OnCollisionEnter(Collision other)
     {
             if (other = "Abang");
         {
             bang.clip = asteroidbang [Random.Range (0, 3)]; 
             bang.Play ();
         }
     }
 } 

All your help is highly appreciated.

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

2 Replies

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

Answer by sas_88 · May 13, 2015 at 12:02 PM

I have checked the above code in start function it works fine.

 bang = GetComponent<AudioSource> ();
 int ran = Random.Range (0, 3);
 bang.clip = audio1 [ran]; 
 bang.Play ();

check OnCollisionEnter(Collision other) function.

use this if (other.gameobject.name == "Abang") instead of if (other = "Abang");

and check the condition satisfies.

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 Mimk · May 13, 2015 at 03:05 PM 0
Share

Dude, thank you very much!!! now it is working beautifully.

this is the final code;

 using UnityEngine;
 using System.Collections;
 
 public class AsteroidBang : $$anonymous$$onoBehaviour 
 {
     public AudioClip[] asteroidbang;
 
     private AudioSource bang;
 
     void Awake()
     {
         bang = GetComponent<AudioSource> ();
         int ran = Random.Range (0, 3);
         bang.clip = asteroidbang [ran]; 
         bang.Play ();
     }
 }
avatar image
1

Answer by Ekta-Mehta-D · May 13, 2015 at 11:01 AM

hii..

Try to use

  if(other.name.Equals("Abang"))
  {
      bang.PlayOneShot(asteroidbang [Random.Range (0, 3)]);
  }

 


Hope this will help you.

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 Mimk · May 13, 2015 at 02:02 PM 0
Share

Hello, it did not work, I made some changes to the code and it still is not working, this is my code right now;

using UnityEngine; using System.Collections;

public class AsteroidBang : $$anonymous$$onoBehaviour { public AudioClip[] asteroidbang;

private AudioSource bang;

void Awake() { bang = GetComponent (); }

void Update()

{ bang.clip = asteroidbang [Random.Range (0, 3)]; bang.PlayOneShot(asteroidbang [Random.Range (0, 3)])); } }

Now I get...some sound to play when the asteroid explode, but the sound is just crazy, it is glitchy and it's like looping on points where it should not (it has the loop check off).

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

AudioClip[] bug 1 Answer

Check scilence in audio clip using FFT. 0 Answers

Make the audio manager select from an array of sounds 1 Answer

Play Audio in sequence from children objects 3 Answers

Randomize Audio Clip 1 Answer


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