- Home /
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.
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.
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 ();
}
}
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.
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
Follow this Question
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