Search Unity

Audio Clip Trigger

Discussion in 'Scripting' started by stevedarby02, Feb 21, 2015.

  1. stevedarby02

    stevedarby02

    Joined:
    Jan 4, 2013
    Posts:
    61
    Hi,

    Could someone help me out with the script below? Can't quite see why this isn't working.

    If i comment out the else statement it works, but the wrong way round. Once i let go of the 'vertical' button the audio plays and then stops once i click it again. I want it the other way round, but it should already be doing that...

    Any ideas?

    Thanks

    Code (JavaScript):
    1.  
    2. var forestAudio : AudioClip;
    3. var enteredArea : boolean = false;
    4.  
    5. function Update (){
    6. var Audio = gameObject.GetComponent(AudioSource);
    7. Audio.clip = forestAudio;
    8.     if(Input.GetButton("Vertical") && enteredArea == true)
    9.      {
    10.       Audio.Play();
    11.      }
    12.      else
    13.       {
    14.        Audio.Pause();
    15.       }
    16. }
    17.  
    18. function OnTriggerEnter (col : Collider)
    19. {
    20.         if(col.gameObject.name == "GrassSound")
    21.           {
    22.              enteredArea = true;          
    23.           }    
    24. }