When Tween effect or Animation is used for menu item, audio sounds like EERRRRRRRRRRRRR

Hello, i’m having bit of an odd issue. I recently posted on the scripts forum on how to achieve a fade in and out effect for text.
I figured out two ways. Using DOTween or creating an animation. The same weird thing happens when trying both of these solutions. When my GUI comes up for when the player dies, a sound is played. That sound plays and sounds like, ERRRRRRRRRR… like it’s playing the first note of the clip many times over and over again.

Here’s my script that controls the GUI functions

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class InGameGUI : MonoBehaviour {
    public GameObject UIPanelPause;
    public GameObject UIPanelWin;
    public GameObject UIPanelLose;
    public GameObject scoreText;
    public GameObject fuelMeterGUI;

    public AudioClip winClip;
    public AudioClip loseClip;

    public int TotalLZ;

    public string NextLevel;

    private ASyncLoadingScreen ASyncLS;

    private int NumLZActivated;
    private int levelScore;

    private float fuelMeterStartingWidth;

    private string GUImode = "gaming";

    // Use this for initialization
    void Start ()
    {
        ASyncLS = GameObject.FindWithTag("ASyncLS").GetComponent<ASyncLoadingScreen>();
        Time.timeScale = 1.0f; //Just to make sure time is proper
        //fuelMeterStartingWidth = fuelMeterGUI.GetComponent<RectTransform>().rect.width;
        scoreText.GetComponent<Text>().text = "SCORE - " + NumLZActivated + "/" + TotalLZ;
        //Debug.Log("Hello world");
    }
   
    // Update is called once per frame
    void Update ()
    {
        if(Input.GetKeyDown("escape") && GUImode == "gaming") //Pause game when escape is pressed
        {
            Time.timeScale = 0.0f;
            GUImode = "paused";
            //print("paused");
            UIPanelPause.SetActive(true);
        }
        else if (Input.GetKeyDown("escape") && GUImode == "paused") //Resume game when escape is pressed
        {
            Time.timeScale = 1.0f;
            GUImode = "gaming";
            //print("gaming");
            UIPanelPause.SetActive(false);           
        }
        /*if (NumLZActivated == TotalLZ)
        {
            GUImode = "win";
        } */      
        if(GUImode == "win")
        {
            Time.timeScale = 0.0f;
            GUImode = "gaming";
            UIPanelWin.SetActive(true);
        }
        if(GUImode == "lose")
        {
            StartCoroutine(LoseGUI());           
        }
        if (GUImode == "lose" && Input.GetKeyDown("space"))
        {
            OnRetryLevel();           
        }
       
    }
    public void OnQuit()
    {
        GUImode = "gaming";
        Time.timeScale = 1.0f;
        Application.LoadLevel("MainMenu");
    }
    public void OnNextLevel()
    {
        GUImode = "gaming";
        Time.timeScale = 1.0f;
        //Application.LoadLevel(Application.loadedLevel + 1);
        ASyncLS.RunLoadingScreen(NextLevel);
    }
    public void OnRetryLevel()
    {
        GUImode = "gaming";
        Time.timeScale = 1.0f;
        Application.LoadLevel(Application.loadedLevel);
    }
    public void LZActivated()
    {
        //levelScore += 500;
        NumLZActivated++;
        //scoreText.GetComponent<Text>().text = "SCORE - " + levelScore;  Old Method
        scoreText.GetComponent<Text>().text = "SCORE - " + NumLZActivated + "/" + TotalLZ;
        if(NumLZActivated == TotalLZ)
        {
            Win();
        }
    }
    public void Win()
    {
        GetComponent<AudioSource>().clip = winClip;
        GetComponent<AudioSource>().Play();
        GUImode = "win";
        PlayerPrefs.SetInt("playerLevel",Application.loadedLevel+1);
        PlayerPrefs.Save();
    }
    public void Lose()
    {
        GUImode = "lose";
    }
    IEnumerator LoseGUI()
    {
        yield return new WaitForSeconds(2.3f);
        //Debug.Log("Entered LoseGUI and attempting to play sounds");
        GetComponent<AudioSource>().clip = loseClip;
        GetComponent<AudioSource>().Play();
        //Time.timeScale = 0.0f;
        //GUImode = "gaming";
        UIPanelLose.SetActive(true);       
    }
    public void UpdateFuelMeter(float newFuelAmount, float fuelStartAmount)
    {
        //change the GUI to decrease in fuel.
        //fuelMeterGUI.GetComponent<RectTransform>().sizeDelta = new Vector2((fuelMeterStartingWidth * (newFuelAmount * 0.005f)), 20);
        fuelMeterGUI.GetComponent<Image>().fillAmount = newFuelAmount / fuelStartAmount;
        fuelMeterGUI.GetComponent<Image>().color = Color.Lerp(Color.red, Color.green, newFuelAmount / fuelStartAmount);
    }
}

Line 117 is the function where i call the sounds for the GUI and then i check in the Update() function for when GUImode = lose

I would also like to note that i have BG music playing. This music doesn’t have the same problem as the one this script plays.

Thanks for any help. Let me know if you need anything else from me.
-Jon

if(GUImode == "lose")
 {
     StartCoroutine(LoseGUI());         
 }

So, is that if statement true indefinitely? Because then you are playing the audio every frame.

@Hikiko66 I guess i should mention that when i’m not trying to achieve the fade in and out text effect, the audio plays just fine. It does not play indefinitely.

Try this

IEnumerator LoseGUI()
   {
        GUImode = "gaming";
        yield return new WaitForSeconds(2.3f);
       //Debug.Log("Entered LoseGUI and attempting to play sounds");
        GetComponent<AudioSource>().clip = loseClip;
        GetComponent<AudioSource>().Play();
       //Time.timeScale = 0.0f;
        UIPanelLose.SetActive(true); 
   }
1 Like

Oh my… why did i ever comment that out? That resolved the issue. I also see why now. That if statement was being called continuously to play the clip. I wonder why it worked fine when i wasn’t trying to have the text fade in and out. Strange but okay.

Thanks for helping!