Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Problem playing/opening an audiofile

Discussion in 'Scripting' started by BrianBoolman, Jan 22, 2014.

  1. BrianBoolman

    BrianBoolman

    Joined:
    Jan 22, 2014
    Posts:
    1
    Hey there, this is my first post on this forum and I hope you can help me out.

    I'm working on a script for the menu/intro of a game I'm making and I ran into a small problem with my audio files. After I've faded the music for the menu, I want to play another audio file. This audio file loads, and the volume is set back to 1, but nothing's happening. The file doesn't play and after closer inspection I see the volume sometimes briefly switching back to another random value and sometimes it briefly loads the previous audio file and then switches back to the one I want to play. I hope someone can make a recommendation on what to change in my script to get things working. Here’s my script:

    //start level with no GUI
    private var showGUI : boolean = false;
    //GUI textures/screens
    var scherm1 : Texture;
    var scherm2 : Texture;
    var scherm3 : Texture;
    //audiofiles
    var AudioFile : AudioClip;
    var AudioFile2 : AudioClip;
    //var i is used in for fading the audio, var c is used to open new textures
    var i : int;
    var c : int = 0;


    function Start (){
    //start level playing the first audiofile with volume 1
    audio.clip = AudioFile;
    audio.volume = 1;
    audio.Play();
    }

    function Update (){
    //start the function in which the GUI textures are shown
    showGUI = true;
    //eachtime e is pressed, 1 is added to var c, thus opening the next GUI texture
    if (Input.GetKeyDown ("e")){
    c++;
    }
    //when var c reaches 2, load function AudioFade
    if (c==2){
    AudioFade ();
    }
    }

    function OnGUI(){
    //when started, load the first GUI texture and make it fullscreen
    GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), scherm1);
    //the first time e is pressed, load the second GUI texture and make it fullscreen
    if (c==1){
    GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), scherm2);
    }
    //the third time e is pressed, load the third GUI texture and make it fullscreen
    if (c==2){
    GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), scherm3);
    }
    }

    function AudioFade (){
    //when started, select the first audiofile
    audio.clip = AudioFile;
    //count down var i and use it to fade the audiovolume
    for (i = 4.5; i > 0; i--){
    audio.volume = i * .1;
    //wait for 1 second, then stop the first audiofile
    yield new WaitForSeconds (1);
    audio.Stop();
    //wait for 1 second, then open the second audiofile and play it with volume 1
    yield new WaitForSeconds (1);
    audio.clip = AudioFile2;
    audio.volume = 1;
    audio.Play();
    //wait for 1 second, then load the next level
    yield new WaitForSeconds (1);
    //Application.LoadLevel ("1");
    }
    }