Fade in AudioLister over time #c

I’m trying to fade in the audioLister over time but i cant seem to get it to work.

public class Opening : MonoBehaviour {
	
	public Transform player;
	public GUISkin skin;
	public GUISkin skinTwo;
	bool closedCapOne = false;
	bool closedCapTwo = false;
	bool closedCapThree = false;
	bool closedCapFour = false;
	bool closedCapFive = false;
	bool closedCapSix = false;
	bool title = true;
	float size = 20;
	float startT;
	float endT;

	void Start () {
		StartCoroutine(Open());
	}
	
	void Update () {
		size += 10 * Time.smoothDeltaTime;
	}
	
	IEnumerator Open () {
			AudioListener.volume = 0.0f;
			CameraFade.FadeOutMain(0.1f);
		yield return new WaitForSeconds (5);
			closedCapOne = true;
			title = false;
		yield return new WaitForSeconds (8);
			closedCapOne = false;
			closedCapTwo = true;
		yield return new WaitForSeconds (3);
			closedCapTwo = false;
			closedCapThree = true;
		yield return new WaitForSeconds (2);
			closedCapThree = false;
			closedCapFour = true;
		yield return new WaitForSeconds (2);
			closedCapFour = false;
			closedCapFive = true;
		yield return new WaitForSeconds (4);
			closedCapFive = false;
			closedCapSix = true;
		yield return new WaitForSeconds (5);
			// fade in sound
			startT = Time.time;
			endT = Time.time + 2f;
			AudioListener.volume = Mathf.Lerp(0.0f, 1.0f, (Time.time - startT) / (endT - startT) );
			// --
			CameraFade.FadeInMain(3);
		yield return new WaitForSeconds (2);
			closedCapSix = false;
	}

How would you go about this?

I’m not really sure what you’re trying to achieve here, but your problem is going to be that you’re only calling the volume update once, after 5 + 8 + 3 + 2 + 2 + 4 + 5 seconds.

You probably want to do something like:

float t = 0;
while(t < 2f) {
    AudioListener.volume = Mathf.Lerp(0.0f, 1.0f, t);
    yield return null;
    t+=Time.deltaTime;
}
AudioListener.volume = Mathf.Lerp(0.0f, 1.0f, 1);

Note that if all those bools are necessary you can probably replace them with a simple enum (state machine), or an int (currentClosedCap)