Hi everyone…so I am kind of new to unity and was trying to fade multiple sound tracks in my scene.Everytime I attach the script to my gameobject and run it Unity crashes.Attaching my script below->
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Fade_Play_Audio : MonoBehaviour
{
private AudioSource audioS;
private bool keepFadeIn;
private bool keepFadeOut;
// Start is called before the first frame update
void Start()
{
audioS = FindObjectOfType<Audio_Manager>().GetComponent();
FindObjectOfType<Audio_Manager>().play(“First_Floor”);
FadeInCaller(“First_Floor”, .1f, 100);
}
//coroutines
public IEnumerator FadeIn(string name, float speed, float maxVolume)
{
Debug.Log(2);
keepFadeIn = true;
keepFadeOut = false;
audioS.volume = 0;
while(audioS.volume<maxVolume && keepFadeIn)
{
Debug.Log(audioS.volume);
audioS.volume += speed;
}
yield return new WaitForSeconds(0.1f);
}
public IEnumerator FadeOut(string name, float speed)
{
keepFadeIn = false;
keepFadeOut = true;
while (audioS.volume>=speed && keepFadeOut)
{
audioS.volume -= speed;
}
audioS.volume = 0;
yield return new WaitForSeconds(0.1f);
}
//coroutine callers
public void FadeInCaller(string name, float speed, float maxVolume)
{
Debug.Log(2);
StartCoroutine(FadeIn(name,speed,maxVolume));
}
public void FadeOutCaller(string name, float speed)
{
StartCoroutine(FadeOut(name, speed));
}
void OnTriggerEnter(Collider col)
{
if (col.tag == "Player" && gameObject.tag == "Door1")
{
FadeOutCaller("First_Floor", .2f);
}
if (col.tag == "Player" && gameObject.tag == "Door2")
{
FindObjectOfType<Audio_Manager>().play("Bottom_Floor");
FadeInCaller("Bottom_Floor", .2f, 100);
}
if (col.tag == "Player" && gameObject.tag == "Boss_Room_Door")
{
//play music
}
}
}