- Home /
My gunFire sound starts at the startup.
I unticked the "Play On Awake" option, so I think that the problem is in the script. Here's it:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HandgunFire : MonoBehaviour
{
public GameObject theGun;
public GameObject muzzleFlash;
public AudioSource gunFire;
public bool isFiring = false;
public AudioSource emptySound;
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
if (GlobalAmmo.handgunAmmo < 1)
{
emptySound.Play();
}
else
{
if (isFiring == false)
{
StartCoroutine(FiringHandgun());
}
}
}
}
IEnumerator FiringHandgun()
{
GlobalAmmo.handgunAmmo -= 1;
isFiring = true;
theGun.GetComponent<Animator>().Play("HandgunFire");
muzzleFlash.SetActive(true);
gunFire.Play();
yield return new WaitForSeconds(0.05f);
muzzleFlash.SetActive(false);
yield return new WaitForSeconds(0.15f);
theGun.GetComponent<Animator>().Play("New State");
isFiring = false;
}
}
P.S I don't speak English very well so I hope I didn’t say anything wrong.
Answer by tkati · Feb 20, 2021 at 11:33 AM
Do you have more then one object using the same audio source? Each would need to be toggled.
Yes, I have more than one object using gunFire but how can I toggle it in each object?
I don't believe the audio source asset has a toggle, thus via the Inspector would have to Uncheck each time add to an object, or use a script to loop through the active game objects, probably using the tag then checking for audio or specific audio source and toggle it off. Or if only certain objects have gun, in start function suppose could check if audio source play on awake is off.
audioOfBoltShot = GetComponent<AudioSource>();
audioOfBoltShot.playOnAwake = false;
/////////////////////////////////////////////////////////////////////////////////////////// //using your example probably be more like
void Start(){
gunFire = GetComponent<AudioSource>();
gunFire.playOnAwake = false;
}
edit: if still doing it via script there is also an awake function might have to be done in there
void Awak(){ gunFire = GetComponent(); gunFire.playOnAwake = false; }
When I untick the isFiring property, the sound doesn't start at the startup but I can't shoot.
Your answer
Follow this Question
Related Questions
Why the audio sound effects of the main menu are not hearing like in mute ? 0 Answers
Glitched Music 0 Answers
How can i set audio mixer groups volume to be used with ui slider when 0 is lower and 80 louder ? 1 Answer
How can i make my soldier to patrol over the space station in random ? 1 Answer
How can i move object between given random positions ? 0 Answers