Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Coroutine and SetFloat not working as expected

Discussion in 'Scripting' started by rookie_92, May 29, 2017.

  1. rookie_92

    rookie_92

    Joined:
    May 1, 2017
    Posts:
    1
    I have set up a Coroutine to fade a mixer channel in and out depending on being within a trigger using a lerp to fade over time. The fade-in is working correctly but when I leave the trigger area the mixer is not updating.

    I have debugged and the exposed parameter, arpVol, is apparently being updated by SetFloat but nothing changes on the actual mixer itself.




    public AudioMixer masterMixer;

    public float minimum = -80f;
    public float maximum = 0f;
    public float speedOfIncrease = 0.08f;

    public float arpVol;

    //private void SetArpVol(float arpVol)
    //{
    // masterMixer.GetFloat("arpVol", out arpVol); //pulls value from Arp mixer attenuation
    //}


    void OnTriggerEnter(Collider coll)
    {
    Debug.Log("trigger entered");

    StartCoroutine(fadeInSound());
    StopCoroutine(fadeOutSound());


    }

    void OnTriggerExit(Collider coll)
    {
    Debug.Log("trigger exited");

    StartCoroutine(fadeOutSound());
    StopCoroutine(fadeInSound());


    }



    IEnumerator fadeInSound()
    {
    Debug.Log("fadeinsound()");

    var originalValue = 0F;
    masterMixer.GetFloat("arpVol", out originalValue);
    var speedLocal = speedOfIncrease;

    // -80 to 0
    while (arpVol <= 0f)
    {
    Debug.Log("fadeinsound() while");
    masterMixer.SetFloat("arpVol", Mathf.Lerp(originalValue, maximum, speedLocal));
    speedLocal += 0.1f * Time.deltaTime;
    yield return null;
    }
    }

    IEnumerator fadeOutSound()
    {
    Debug.Log("fadeOUTsound()");

    // 0 to -80
    var originalValue = 0F;
    masterMixer.GetFloat("arpVol", out originalValue);
    var speedLocal = speedOfIncrease;

    while (arpVol >= -80f)
    {
    Debug.Log("fadeoutsound() while");
    var d = 0F;
    masterMixer.GetFloat("arpVol", out d);
    Debug.Log("mixer varpvol is now: " +d);

    var increase = Mathf.Lerp(originalValue, minimum, speedLocal);
    //Debug.Log("increase over time: " + increase);

    masterMixer.SetFloat("arpVol", increase);
    speedLocal += 0.1f * Time.deltaTime;

    if (arpVol >= -70)
    arpVol = -80;

    yield return null;
    }
    }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    https://forum.unity3d.com/threads/using-code-tags-properly.143875/
    Use code tags...

    It's hard to follow honestly(lack of code tags makes it harder), but you may want to look at how stopcoroutine actually works.

    https://docs.unity3d.com/ScriptReference/MonoBehaviour.StopCoroutine.html

    You assign a coroutine to a variable and then start and stop that variable. This is the only way I got it to stop properly as well. But my guess is the reason it doesn't work on exit is because you never stop the enter code from running.

    You can test this by putting debugs into both while statements and see if the fadeInSound does actually stop correctly when you exit.