Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 beatdown0312 · Apr 25, 2020 at 05:40 PM · audiofpsgun script

Weapon Audio Playing When Swapping Guns

I created a gun script allowing my gun to fire as well as a weapon swap script, but when I swap weapons it plays the noise.

 using UnityEngine;
 
 public class WeaponSwap : MonoBehaviour
 {
 
     [SerializeField] int selectedWeapon = 0;
     
     // Start is called before the first frame update
     void Start()
     {
         SelectWeapon();
     }
 
     // Update is called once per frame
     void Update()
     {
 
         int previousSelectedWeapon = selectedWeapon;
 
         if (Input.GetAxis("Mouse ScrollWheel") > 0)
         {
             if (selectedWeapon >= transform.childCount - 1)
                 selectedWeapon = 0;
             else
                 selectedWeapon++;
         }
 
         if (Input.GetAxis("Mouse ScrollWheel") < 0)
         {
             if (selectedWeapon <= 0)
                 selectedWeapon = transform.childCount - 1;
             else
                 selectedWeapon--;
         }
 
         if (Input.GetKeyDown(KeyCode.Alpha1))
         {
             selectedWeapon = 0;
         }
 
         if (Input.GetKeyDown(KeyCode.Alpha2))
         {
             selectedWeapon = 1;
         }
 
         if (previousSelectedWeapon != selectedWeapon)
         {
             SelectWeapon();
         }
     }
 
     void SelectWeapon()
     {
         int i = 0;
         foreach (Transform weapon in transform)
         {
             if (i == selectedWeapon)
                 weapon.gameObject.SetActive(true);
             else
                 weapon.gameObject.SetActive(false);
             i++;
         }
     }
 }
 
 using UnityEngine;
 
 public class Gun : MonoBehaviour
 {
     [SerializeField] float damage = 10f;
     [SerializeField] float range = 100f;
     [SerializeField] float fireRate = 15f;
     [SerializeField] float force = 30f;
 
     [SerializeField] AudioClip fireNoise;
     [SerializeField] Camera fpsCam;
     [SerializeField] ParticleSystem muzzleFlash;
     [SerializeField] GameObject impactEffect;
 
     private float nextTimeToFire = 0f;
     public AudioSource source { get { return GetComponent<AudioSource>(); } }
 
     private void Start()
     {
         gameObject.AddComponent<AudioSource>();
         source.clip = fireNoise;
     }
 
     void Update()
     {
         if (Input.GetButtonDown("Fire1") && Time.time >= nextTimeToFire)
         {
             nextTimeToFire = Time.time + 1f / fireRate;
             Shoot();
             PlaySound();
         }
     }
 
     void Shoot()
     {
         RaycastHit hit;
         muzzleFlash.Play();
         if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
         {
 
             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);
         }
     }
     void PlaySound()
     {
         source.PlayOneShot(fireNoise);
     }
 }
 

I tried removing the PlaySound() from the update but that didn't stop it, so I put it back and tried moving the audio clip selection from start() to update() and that didn't work either.

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
Best Answer

Answer by ncortiz · Apr 25, 2020 at 05:59 PM

@beatdown0312

If each weapon is a different gameobject with a "Gun" behavior attached to it, then the problem is that you're creating a audiosource attached to each gun gameobject (in Gun.cs start).


Then your gun switching script is disabling the gameobjects of whatever weapons are not active which causes the audiosources of each one of them to be disabled immediately as well.


To solve it simply use one audiosource attached to an object that isn't disabled by the weapon swap script.


Or simply have a permanent audiosource attached to the gameobject where you add the weapon switching script.


Also, adding GetComponent to a getter (get source) is basically the same as using GetComponent and since you're using it every time you PlaySound() (every time you shoot). Instead create a "source" variable and use that instead.


Comment
Add comment · Show 1 · 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 beatdown0312 · Apr 25, 2020 at 06:47 PM 0
Share

Thanks that got it working. I tried following a tutorial but that didn't apply as well I guess.

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

200 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

Related Questions

I don't understand, the following coding is my recoil coding for c sharp. 1 Answer

How to add a subtitle to my game 2 Answers

Many Audio Listeners to multiplayer?? How? 1 Answer

audio slows down my game (dramatically) 0 Answers

how to keep 1 audio listener 1 Answer


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