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

Sound repeating too fast

Discussion in 'Scripting' started by ROSSCOGamesOfficial, Jun 18, 2015.

  1. ROSSCOGamesOfficial

    ROSSCOGamesOfficial

    Joined:
    Jun 4, 2014
    Posts:
    19
    This is supposed to repeat every second until the animation stops playing, but the sound repeats every frame! Here's my code (in JS):

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var chopSound: AudioClip;
    4. var isPlaying = false;
    5.  
    6. function AnimPlay () {
    7. animation.Play();
    8. while (animation.isPlaying){
    9. yield WaitForSeconds(1);
    10. audio.PlayOneShot(chopSound);
    11. yield WaitForSeconds (1);
    12. }
    13. }
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    are you calling AnimPlay() from Update somewhere? can we see that code?
     
  3. ROSSCOGamesOfficial

    ROSSCOGamesOfficial

    Joined:
    Jun 4, 2014
    Posts:
    19
    Yes, I am calling it through a different script with the SendMessage code. Here's my other script:
    Code (JavaScript):
    1. #pragma strict
    2. var Player: GameObject;
    3. var Lumber: GameObject;
    4. var Sapling: GameObject;
    5. var Range: float = 5;
    6. var ChopTimer: float = 5;
    7. var InstantiateCount: float = 1;
    8. var Colliders: Collider[];
    9. function OnMouseOver () {
    10. Colliders = Physics.OverlapSphere(transform.position, Range);
    11.  
    12. if (Input.GetMouseButton(0)){
    13. for (var hit in Colliders){
    14. if (!hit){
    15. continue;
    16. }
    17.  
    18. if (hit.transform.name == "First Person Controller"){
    19. ChopTimer -= Time.deltaTime;
    20. Player.gameObject.SendMessage("AnimPlay");
    21. }
    22. }
    23.  
    24. if (ChopTimer <= 0 && InstantiateCount == 1){
    25. Destroy(transform.gameObject);
    26. ChopTimer = 5;
    27. InstantiateCount = 0;
    28. Refill();
    29. }
    30. }
    31. }
    32.  
    33. function Update () {
    34. Debug.Log(ChopTimer);
    35. }
    36.  
    37. function Refill () {
    38. InstantiateCount = 1;
    39. }
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  5. ROSSCOGamesOfficial

    ROSSCOGamesOfficial

    Joined:
    Jun 4, 2014
    Posts:
    19
    Thank you, I'll try to figure it out from here!