I am new bie to unity. In my project i have a sound gameobject . In that sound game object i have added 20 audio source. Each audio source will play the sounds. In that i have to make particular sounds high and particular sounds low. How can i do it.
for eg. in the sound game object i have to make 4 th audisource sound should high and 10th audio source should less.
Thanks alucards for your reply. Actually in my project
using UnityEngine;
using System.Collections;
public class Sounds : MonoBehaviour {
// Use this for initialization
public AudioClip longsound1,longsound2,shortsound1,shortsound2;
AudioSource AudioSObj;
public int select;
void Start () {
AudioSObj = this.gameObject.GetComponent<AudioSource> ();
}
// Update is called once per frame
void Update () {
if(select==1)
{
Debug.Log (" I am in 1");
AudioSObj.PlayOneShot(longsound1,1f);
select=5;
}
if(select==2)
{
AudioSObj.PlayOneShot(Shortsound1,1f);
select=8;
}
}
}
in my project if i press 1 the long sound should play. It will play for 30 seconds. If i press 2 the short sound should play. while playing shortsound the longsound1 volume should less. the short sound should high. how can i do it.
my question. i have to play two sounds. While playing second sounds, first sound should reduce the volume. how can i do it.
Change the second argument where you have 1f to the volume you need. 1f is 100% volume , .5f is 50%, 0f is 0%, etc.
Here’s code from my project playing sounds at 90% and 70%
public AudioSource SoundSrc;
public void PlayBonusTime() { if (PlaySounds) SoundSrc.PlayOneShot(SoundBonusTime, 0.9F); }
public void PlaySugarFoot() { if (PlaySounds) SoundSrc.PlayOneShot(SoundSUGARFOOT, 0.7F); }
Thanks Loves Solving Problmes for your reply.I have found a solution outside of the forum May be the solution may in the forum . But i cant found it. I have attached the code.
using UnityEngine;
using System.Collections;
public class Sounds : MonoBehaviour {
// Use this for initialization
public AudioClip longsound,shortsound;
public int select;
public AudioSource[] allAudioSources;
void Start () {
allAudioSources =
UnityEngine.Object.FindObjectsOfType<AudioSource>();
allAudioSources[0].PlayOneShot(FullSound);
}
// Update is called once per frame
void Update () {
if(select==0)
{
allAudioSources[0].PlayOneShot(longsound);
select=20;
}
if(select==1)
{
allAudioSources[0].Volume=0.2f;
allAudioSources[1].PlayOneShot(shortsound);
select=20;
}
}
while playing shortsound the longsound1 volume should less : The problem I see is modifying the volume of a PlayOneShot object without having a reference to it. You need to know what object to change any variables on.
I have not tried it, but imagine you could use an AudioMixer. I assume all instantiated PlayOneShot objects inherit parameters of the AudioSource that spawn them. [Unity Learn AudioMixing]
Each AudioSource has a sound Clip and an AudioMixer. Then control the volume of the mixer channel in code. [Exposing Mixer parameters to Code]
Alternatively, you could write your own PlayOneShot (just like in the PlayClipAtPoint examples here). Store all played notes in a list, iterate through list and modify volumes (check for and remove nulls).
Experiment with the mixer and reply your findings.