Search Unity

Why doesn't my sound function work here but not there?

Discussion in 'Audio & Video' started by JamieJamJams, Nov 17, 2015.

  1. JamieJamJams

    JamieJamJams

    Joined:
    Apr 7, 2015
    Posts:
    8
    Here is my code.

    I'm trying to figure out why my PlaySound(#); functions work when they are in the "Pick Up" compare tag IF statement, but they don't work when the PlaySound(#); function is placed in the "Finish" IF statement. I get all the debug logs working in the "Finish" statement. So the rest of the code is working, just not the sounds.

    I tested the sound files and its not the file.

    This script is on my player and is supposed to play sounds when he hits other 2D collider triggers.

    Any ideas? I'm somewhat a beginner, so please offer a more elaborate answer if you can. Thank you.

    public voidOnTriggerEnter2D (Collider2D other)
    {
    //Sound function works here
    if (other.gameObject.CompareTag ("Pick Up"))
    {
    other.gameObject.SetActive (false);
    Debug.Log("YOU GOT A STICKER");
    PlaySound (0);
    score = score + 1;
    setScoreText();

    }

    //Sound function works here
    if (other.gameObject.CompareTag ("Enemy"))
    {
    Debug.Log ("Enemy hit");
    PlaySound (1);
    score = score - 1;

    if(score <= 0)
    {
    score = 0;
    }
    other.gameObject.SetActive (false);
    setScoreText();
    }

    //Sound function does not work here
    if(other.gameObject.CompareTag ("Finish"))
    {
    Debug.Log("FinishLine");

    if(score >=5)
    {
    PlaySound(3);
    Debug.Log("score is 5 or above");

    }

    if(score >=2 && score < 5)
    {
    PlaySound(4);
    Debug.Log("score is below 5");

    }
    }

    }


    voidPlaySound(intclip)
    {
    AudioSourceaudio = GetComponent<AudioSource>();
    audio.clip = audioClip[clip];
    audio.Play ();
    }