Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by unity_PhVxmXvHqYCxRQ · Feb 20, 2021 at 11:14 AM · scripting problemaudioscript.startupsounds

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image unity_PhVxmXvHqYCxRQ · Feb 20, 2021 at 11:43 AM 0
Share

What do you mean?

avatar image unity_PhVxmXvHqYCxRQ · Feb 20, 2021 at 11:44 AM 0
Share

Yes, I have more than one object using gunFire but how can I toggle it in each object?

avatar image tkati · Feb 20, 2021 at 11:50 AM 0
Share

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; }

avatar image unity_PhVxmXvHqYCxRQ · Feb 20, 2021 at 03:35 PM 0
Share

I am sorry but it doesn't work :(

avatar image unity_PhVxmXvHqYCxRQ · Feb 20, 2021 at 03:51 PM 0
Share

When I untick the isFiring property, the sound doesn't start at the startup but I can't shoot.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

251 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges