Hi Forum, I have a bit of a mystery on my hands that I can’t seem to figure out. I found an audio fader script earlier that was written in Javascript, and I’ve been converting it to C#. I finally got it to work, but I don’t quite understand why what I’ve done works. Here is the code:
Now I originally had no print line in the Pause function, and when I called FadeAudio the volume instantly went to zero. Then when I added the print line, it finally caused the delay I was wanting. Does anyone know why that is?
Also, I tried changing the yield line to:
yield return new WaitForSeconds(2);
just to see what would happen, and it causes a similar fade. I was thinking that when doing a yield with WaitForSeconds, code execution basically stops until the yield returns after x amount of seconds each time, is that not the case though?
the problem is your code is meant to go to 0 instantly because you fully loop it through!
the function where you want to wait must be the IEnumerator thats executed through StartCoroutine so in your case the fade and in there you need to add a yield return null; (to wait till next frame) instead of the coroutine call for pausing which does nothing actually as the fade still does not wait.
the delay is just a consequence of you trash flooding the console.
So you’re saying that the yield needs to be in the while loop, and the FadeAudio function has to be the IEnumerator? I was actually trying that at first, but when I tried calling the function from another script it wouldn’t execute. Then I saw that the IEnumerator function has to be called using StartCoroutine, so I tried:
and I stuck a print call in the while loop, but it looks like the loop is only executed once? Does the return part of the yield cause the loop to break out, and if so, how do I get it to pause while still looping through normally?
and no the return part is required, otherwise yield does not work at all in C# unless you use it to yield StartCoroutine(…)
pausing a function without pausing the whole application is done through yield return new xxxx or yield return null in case you want to pause it till next update what you normally want to do.
Yeah after doing more testing it looks like the Mathf.Lerp is jumping right to the end for some reason. At least I’m getting closer though, thanks for the help