Search Unity

GameObject walk-through Button

Discussion in 'Scripting' started by gul, Mar 27, 2008.

  1. gul

    gul

    Joined:
    Jan 28, 2008
    Posts:
    25
    Hi everybody,
    I wonder if it's possible to use a gameObject like a button for an audio.clip .
    Explanation :
    I would like to play a sound when I walk through an empty game object for the first time.
    I walk through it for the second time, stop the sound.
    Just like a button.

    I try,naturely :
    Code (csharp):
    1.  
    2. var son : AudioClip;
    3. function OnTriggerEnter () {
    4.     audio.clip = son;
    5.     audio.Play();
    6. }
    7.  
    8.  
    But after,I've got no Idea for the "stop()" process...

    Thank you for your attention

    Gul
     
  2. thomasvdb

    thomasvdb

    Joined:
    Feb 28, 2008
    Posts:
    85
    Just a guess but maybe you can make a variabele (boolean) which tells you if the sound is playing...

    Code (csharp):
    1.  
    2. var son : AudioClip;
    3. var playing : boolean = false;
    4.  
    5. function OnTriggerEnter () {
    6.    audio.clip = son;
    7.  
    8.    if(!playing) {
    9.     audio.Play();
    10.     playing = true;
    11.    }
    12.  
    13.    if(playing) {
    14.      audio.Stop();
    15.      playing = false;
    16.    }
    17. }
     
  3. gul

    gul

    Joined:
    Jan 28, 2008
    Posts:
    25
    Thanks for your fast reply !
    But I try your code, the "playing" var checked and not checked, this is the same : the sound is "off"
    Why ?
     
  4. Doleman

    Doleman

    Joined:
    Aug 12, 2005
    Posts:
    87
    Have you attached a AudioSource to the GameObject?
    In that case you can just check the isPlaying flag

    Code (csharp):
    1. var son : AudioClip;
    2.  
    3. function OnTriggerEnter () {
    4.    audio.clip = son;
    5.  
    6.    if(!audio.isPlaying)
    7.     audio.Play();
    8.    else
    9.      audio.Stop();
    10. }
     
  5. gul

    gul

    Joined:
    Jan 28, 2008
    Posts:
    25
    Doleman, you've got the powa ! ^^
    It's okay, an audioSource was assigned to a gameObject, Just only the syntax was wrong !

    Thank you very much

    Gul