Search Unity

on text click

Discussion in 'Scripting' started by Mr.BMS, Dec 18, 2014.

  1. Mr.BMS

    Mr.BMS

    Joined:
    Feb 9, 2014
    Posts:
    9
    Hi! I have request,can you please edit my code.I am making simple soundboard,but i wan't to make code work on audio click,not on mouse enter.Like,when you click on text,changes red and starts to play audio,when audio ends,text changes to color white again.

    #pragma strict

    var isQuitButton = false;

    var sound : AudioClip;

    function OnMouseEnter()
    {
    renderer.material.color = Color.red;
    audio.PlayOneShot(sound);
    }

    function OnMouseExit()
    {
    renderer.material.color = Color.white;
    audio.Stop();
    }
     
  2. Mr.BMS

    Mr.BMS

    Joined:
    Feb 9, 2014
    Posts:
    9
    *Bump
     
  3. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    There isn't any event that happens when a sound is done playing. However, there is a length for the audio clip, so you could use that.
    Code (csharp):
    1.  
    2. function OnMouseDown()
    3. {
    4. renderer.material.color = Color.red;
    5. audio.PlayOneShot(sound);
    6. waitForAudio(audio.clip.length);
    7.  
    8. }
    9. function waitForAudio( time:float){
    10. yield WaitForSeconds(time);
    11. //change color back
    12. renderer.material.color = Color.white;
    13. }
    14.  
     
    Last edited: Dec 19, 2014
  4. Mr.BMS

    Mr.BMS

    Joined:
    Feb 9, 2014
    Posts:
    9
    I need code with OnMouseClick.For javascript please,i am begginer in coding.
     
  5. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    I changed the code for you, but you need to take some tutorials.
     
  6. Mr.BMS

    Mr.BMS

    Joined:
    Feb 9, 2014
    Posts:
    9
    Can you please someone edit code,to make it like,when you click audio 2x it plays at the same time(multisound of one audio)Can you please fix it,i won't these multisounds.And i want when i click somewhere else(except box collider) audio stops sounding.I hope you understood my bad english :)


    #pragma strict

    var isQuitButton = false;

    var sound : AudioClip;

    function OnMouseDown()
    {
    renderer.material.color = Color.red;
    audio.PlayOneShot(sound);
    waitForAudio(audio.clip.length);

    }
    function waitForAudio( time:float){
    yield WaitForSeconds(time);
    //change color back
    renderer.material.color = Color.white;
    }
     
  7. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    If you don't want to actually write a game, then don't. Otherwise, read the manual and do some tutorials.