Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Unity audio trigger

Discussion in 'Audio & Video' started by J-FENG, Jan 26, 2016.

  1. J-FENG

    J-FENG

    Joined:
    Jan 12, 2016
    Posts:
    2
    Hello
    I meet some problem in setting audio trigger. Now, i have 2 cubes, and i give these 2 cubes different audio. i want when FPS controller go to one cube, the audio start, and when FPS controller leave the cube, the audio still keeping playing until FPS controller touch the second cube, and it stop playing the first cube audio, start playing second audio

    #pragma strict
    var st:AudioClip;
    var se:AudioClip;
    GetComponent.<AudioSource>().mute =true;

    function OnTriggerEnter (others:Collider) {
    GetComponent.<AudioSource>().mute = false;
    if(others.tag=="player"){
    GetComponent.<AudioSource>().PlayOneShot(st);
    }
    else{

    if(others.tag=="Finish"){
    GetComponent.<AudioSource>().Stop()
    GetComponent.<AudioSource>().PlayOneShot(se);
    }

    }

    }
    Here is code i wirte..the problem i meet now is , when i touch the second cube, the second audio start playing, but the first one still here, it should be closed..

    Can some one give me some advice on code ?
     
  2. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    Code (JavaScript):
    1. GetComponent.<AudioSource>().mute =true;
    What is this for? Is this statement outside of a function? Is it supposed to be in Update()? It still won't work that way, but I'm trying to figure out why it's there.

    If you want to change the properties of the first cube when interacting with the second one, you need some reference to the first cube object. Maybe have one in the controller script?

    Code (JavaScript):
    1. private var lastCubeTouched     :     Collider;
    2.  
    3. function OnTriggerEnter(other : Collider)
    4. {
    5.  
    6.      if (other.gameObject.tag == "Cube")
    7.      {
    8.           if (lastCubeTouched != null)
    9.           {
    10.                lastCubeTouched.gameObject.GetComponent<AudioSource>().Stop();
    11.                lastCubeTouched = other;
    12.           }
    13. }
    The idea is that your character controller keeps track of the cube that is currently playing so that it can stop playback when it touches another one.

    You might want to add a condition in there to check whether other == lastCubeTouched and in the cube script to check whether audioSource.isPlaying before performing these actions if you want to prevent the audio from restarting after touching the same cube more than once.
     
  3. J-FENG

    J-FENG

    Joined:
    Jan 12, 2016
    Posts:
    2
    haha..yeah..i put outside of the function, because i want when u start program, there is no audio sound unitl u touch the trigger.
     
  4. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    I can see your logic there. Instead, put that statement inside of the Start() function, which is called automatically from each script right when they start (the first frame they are active), before any update.

    Code (JavaScript):
    1. Start()
    2. {
    3.      GetComponent<Audiosource>.mute = true;
    4. }
    Have you had any luck with the rest?