So here is a script i found on the unity forums, and I edited it slightly so it would fade out automatically when loading a level. The script works itself works, but the (if loading level) part i added doesnt work, and now it wont work at all. The objects are not present for some reason when the game loads either. Not sure why, any ideas? Thanks.
enum Fade {In, Out}
var fadeTime = 3;
function Awake () {
DontDestroyOnLoad (transform.gameObject);
}
function Update () {
if (Application.isLoadingLevel){
FadeAudio(fadeTime, Fade.Out);
Destroy (gameObject, 4);
}
}
function FadeAudio (timer : float, fadeType : Fade) {
var start = fadeType == Fade.In? 0.0 : 1.0;
var end = fadeType == Fade.In? 1.0 : 0.0;
var i = 0.0;
var step = 1.0/timer;
while (i <= 1.0) {
i += step * Time.deltaTime;
audio.volume = Mathf.Lerp(start, end, i);
yield;
}
}