- Home /
              This question was 
             closed Jul 08, 2018 at 06:48 PM by 
             MilkyPancack for the following reason: 
             
 
            The question is answered, right answer was accepted
 
               Question by 
               MilkyPancack · Jul 08, 2018 at 12:25 AM · 
                audioplayboolaudio sourceonce  
              
 
              How do I play audio once in Update?
Been trying to make an fps game and I have been trying to add a reload sound but this plays once every frame as it is in the update section. How do I make it play once?
using UnityEngine; using System.Collections;
public class Gun : MonoBehaviour {
 public float damage = 10f;
 public float range = 100f;
 public float fireRate = 15f;
 public int maxAmmo = 30;
 private int currentAmmo;
 public float reloadTime = 1f;
 private bool isReloading = false;
 public Camera fpsCam;
 public ParticleSystem muzzleFlash;
 public GameObject impactEffect;
 private float nextTimeToFire = 0f;
 public Animator animator;
 void Start ()
 {
     currentAmmo = maxAmmo;
 }
 
 void Update () {
     if (isReloading)
     //Play audio source here
     return;
     if(currentAmmo <= 0)
     {
         StartCoroutine(Reload());
         return;
     }
     if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire)
     {
         nextTimeToFire = Time.time + 1f / fireRate;
         Shoot();
         AudioSource shoot = GetComponent<AudioSource>();
         shoot.Play();
     }
 }
 IEnumerator Reload ()
 {
     isReloading = true;
     Debug.Log("Reloading...");
     animator.SetBool("Reloading", true);
     yield return new WaitForSeconds(reloadTime);
     animator.SetBool("Reloading", false);
     currentAmmo = maxAmmo;
     isReloading = false;
 }
 void Shoot()
 {
     muzzleFlash.Play();
     currentAmmo--;
     RaycastHit hit;
     if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit))
     {
         Debug.Log(hit.transform.name);
         Target target = hit.transform.GetComponent<Target> ();
         if (target != null) 
         {
             target.TakeDamage (damage);
         }
         GameObject impactGo = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
         Destroy(impactGo, 2f);
     }
 }
     
}
               Comment
              
 
               
              Answer by TreyH · Jul 08, 2018 at 12:31 AM
Just add a condition for it: 
 private bool isReloading = false;
 private bool playReloadSound = false;
 
 void Update () 
 {
     // ...
     if (isReloading && playReloadSound)
     {
         //Play audio source here
         playReloadSound = false;
     }
     // ...
 }
 
 IEnumerator Reload ()
 {
     // ...
     isReloading = true;
     playReloadSound = true;
     // ...
 }
Follow this Question
Related Questions
Audio continuation through two scenes 3 Answers
Audio issue - Trying Waitforseconds 0 Answers
Audiosurf style audio analysis (unity 3.5) 3 Answers
Audio not playing! 0 Answers
Play Audio from different Object 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                