Fading out before load script not functioning correctly?

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;
    
        }
    
    }

The problem is, since your code is in “Update”, the FadeAudio function is called every frame that Application.isLoadingLevel == true . i.e, its called many times over, whereas you should be calling it only once.

The solution is to either call this function in “Start”, so its called only once, or to manually create a flag that makes sure that FadeAudio is only called once.

Solved my own problem, the object was a child object, so the whole thing got deleted.
Thanks,