- Home /
 
 
               Question by 
               UniluckStudios · Jul 29, 2017 at 06:13 PM · 
                collisionaudiosoundaudiosourcedead  
              
 
              Audio Loops?
In my game I want audio to play when I am dead. I only want the clip to play once but with my current code, it loops. I don't know why. Loop is turned off in the audio source component. Here is my script:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;
public class DestroyPlayer : MonoBehaviour {
 float deathCooldown = 5;
 public bool dead;
 public int value = 1;
 public int yay = 3;
 public AudioSource saw;
 void Start () {
     saw = GetComponent <AudioSource> ();
 }
 void OnCollisionEnter (Collision  col)
 {
     if (col.gameObject.tag == "cup") {
         
         ScorePoints.score += value;
         Destroy (this.gameObject);
     }
     if (col.gameObject.tag == "Death") {
         dead = true;
     } else {
         dead = false;
     }
 }
 void Update()
 {
     if (dead == true) {
         saw.Play ();
         deathCooldown -= yay * Time.deltaTime;
         GameObject.Find ("Cups").GetComponent <CupMovemnt> ().cupVelocity = 0f;
         if (deathCooldown <= 0) {
             SceneManager.LoadScene ("Scene");
         } 
     }
 }
 
               }
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by c4ctus · Jul 29, 2017 at 06:25 PM
Try this:
 bool playSawSound = true;
 
 void Update()
 {
     if (dead == true) {
         if(playSawSound)
         {
             saw.Play ();
             playSawSound = false;
         }
         deathCooldown -= yay * Time.deltaTime;
         GameObject.Find ("Cups").GetComponent <CupMovemnt> ().cupVelocity = 0f;
         if (deathCooldown <= 0) {
             SceneManager.LoadScene ("Scene");
         } 
     }
 }
 
              Your answer
 
             Follow this Question
Related Questions
How to get decibel's value of game's sound? 0 Answers
AudioSource won't play 2 Answers
Can you play a sound at a certain point (time) in the file? 1 Answer
Collide sound 2 Answers
How can I make Audio Reverb Zones effect only certain sounds? 0 Answers