Hi Unity Community iam making a game in unity 3d and playing around with some sounds. what iam trying todo its make a ramdom sound when click or move like in RTS or Moba iam more with modelling and sounds creations but weak with code my friend gave me this script but only works with one sound like move only playing one sound here its my question. How do i make this script to work with select move or attack have a random sounds thanks in advance enjoy the holiday best regard charay using UnityEngine; using System.Collections; using System.Collections.Generic; using RUnit; namespace RUnit { [RequireComponent (typeof (Unit))] public class UnitAudio : MonoBehaviour { public AudioClip selectSound; public AudioClip moveSound; public AudioClip EngineSound; public AudioClip attackSound; public AudioClip hitSound; public AudioClip destroySound; //~ private AudioSource audioSrc; // Use this for initialization void Awake () { //~ audioSrc=gameObject.GetComponent<AudioSource>(); //~ if(audioSrc==null){ //~ audioSrc=gameObject.AddComponent<AudioSource>(); //~ audioSrc.playOnAwake=false; //~ audioSrc.loop=false; //~ } Unit unit=gameObject.GetComponent<Unit>(); if(unit!=null) unit.SetAudio(this); //else DestroyImmediate(this); } public void Select(){ if(selectSound!=null)AudioManager.PlaySound(selectSound);} public void Move(){ if(moveSound!=null)AudioManager.PlaySound(moveSound); AudioManager.PlaySound(EngineSound); } public void Attack(){ if(attackSound!=null)AudioManager.PlaySound(attackSound); } public void Hit(){ if(hitSound!=null)AudioManager.PlaySound(hitSound); } public float Destroy(){ if(destroySound!=null){ AudioManager.PlaySound(destroySound); return destroySound.length; } return 0; } } }
Just create an array filled with audioclips and choose a random one. Code (csharp): using UnityEngine; using System.Collections; using System.Collections.Generic; using RUnit; namespace RUnit { [RequireComponent (typeof (Unit))] public class UnitAudio : MonoBehaviour { public AudioClip selectSound; public AudioClip moveSound; public AudioClip EngineSound; public AudioClip attackSound; public AudioClip hitSound; public AudioClip destroySound; public AudioClip[] clips; // new //~ private AudioSource audioSrc; // Use this for initialization void Awake () { //~ audioSrc=gameObject.GetComponent<AudioSource>(); //~ if(audioSrc==null){ //~ audioSrc=gameObject.AddComponent<AudioSource>(); //~ audioSrc.playOnAwake=false; //~ audioSrc.loop=false; //~ } Unit unit=gameObject.GetComponent<Unit>(); if(unit!=null) unit.SetAudio(this); //else DestroyImmediate(this); } public void Select(){ if(selectSound!=null)AudioManager.PlaySound(selectSound);} public void Move(){ if(moveSound!=null)AudioManager.PlaySound(moveSound); AudioManager.PlaySound(EngineSound); } public void Attack(){ if(attackSound!=null)AudioManager.PlaySound(attackSound); } public void Hit(){ if(hitSound!=null)AudioManager.PlaySound(hitSound); } // new public void RandomPlayClip() { AudioManager.PlaySound(clips[Random.Range(0, clips.length)]); } public float Destroy(){ if(destroySound!=null){ AudioManager.PlaySound(destroySound); return destroySound.length; } return 0; } } } You can fill clips array from the inspector. I hope this helps.
Yes, you can do that with the code above but when you want to make it *weighted* randomness you have to go altering the array and adding / removing elements. Cumbersome. Like if you wanted selectSound to play twice as often as the others from a random pool. You could just pick up our Master Audio plugin which does this and every other conceivable thing you'd need without you having to write any code. It's only $20 for a few more days - normally $75, and it's super optimized and runs on all platforms. Is there a reason you want to do it yourself? We've already implemented everything for your needs and we maintain the code and expand on it so that you don't have to worry about that and can spend your time on other problems. Good luck either way. -Brian
hi Brian i pick up the master audio plugin and play with it the thing its that the plug in and this script dont work with ech other. in master audio plug in i make a group sound with random audio but i try to put the gameobject in my script but it will not work its say this need to be and audio source what will u recommended to do becouse this audioscript i have here its being call from another unitscript so it activated the sound like select and movement for the different unit i have in game thx agian for the reply iam still new with scripting but keep fighting my way to completion of this game
AudioManager.PlaySound(clips[Random.Range(0, clips.length)]); } public float Destroy(){ if(destroySound!=null){ AudioManager.PlaySound(destroySound); return destroySound.length; } return 0; } } } [/CODE] You can fill clips array from the inspector. I hope this helps.[/QUOTE] hi zehreken iam been trying too understand this scripting world and fail totally but i keep fighting until it works.. the script i have here its giving me alot of errors when i wont to make and arrays out of the script public AudioClip [] selectSound; public AudioClip [] moveSound; public AudioClip [] attackSound; public AudioClip [] hitSound; public AudioClip [] destroySound; public void Select() { if(selectSound!=null)AudioManager.PlaySound(selectSound[Random.Range(0, clips.length)]); } i think the error comes from another script that in use to call this audio script its working like this i have this one script for multiply units lets say its rts game so the unit script calling this audio script for audio so i change the audio in this script for different units so that why i try to make the public AudioClip [] selectSound; but fail the script giving me and error and i need to put everthing back to audioclip as they was from the begining iam new with scripting i hope u understand but the underline its that iam crying alot when iam working with script... and i try to buy the masteraudio plug in and that one dont works with this script :/ damn but thanks for the reply iam so noob for the scripting world so i dont see what too do when the answar its right in front of me thanks agian for your reply zehreken
I'm not sure I understand, but please watch our short Youtube videos and then contact us again if you need assistance.