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 caleb_b · Nov 26, 2014 at 04:10 AM · c#gunreload

Need some help with my gun reloading script

I've made a script for reloading my gun. It has a set number of bullets that are in the gun, a set number of bullets that can be in the "clip" at once, and a set number of bullets that are in reserve, like extra magazines. And all of these are public variables so I can put this one script on whatever guns I put in my game and simply set the parameters to whatever the specific gun requires. Here's the script:

 using UnityEngine;
 using System.Collections;
 
 public class GunScript : MonoBehaviour 
 {
     public int ammoCapacity;
     public int ammo;
     public int damage;
     public int bulletsRemaining;
     public GUIText AmmoCounter;
     public bool canFire;
 
     void Start () 
     {
         canFire = true;
     }
 
     void Update () 
     {
         if(ammo <= 0) 
         {
             canFire = false;
             if(bulletsRemaining >= ammo) 
             {
                 Reload();
                 bulletsRemaining -= ammo;
                 ammo = ammoCapacity;
             }
         }
         if(canFire == true) 
         {
             if(Input.GetButton("Fire1"))
             {
                 animation.Play();
                 audio.Play();
                 ammo--;
                 Fire();
             }
         }
         AmmoCounter.text = "Ammo:" + ammo + "/" + bulletsRemaining;
     }
 
     void Fire() 
     {
         float range= Mathf.Infinity;
         Ray ray= Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit hit;
                     
         if(Physics.Raycast(ray, out hit, range))
         {
             if(hit.transform.gameObject.tag == "Building")
             {
                 hit.transform.GetComponent<BuildingHealth>().health -= damage;
             }
         }
     }
 
     IEnumerator Reload() 
     {
         yield return new WaitForSeconds (5);
         canFire = true;
     }
 }

The goal is to have the player shoot till the magazine is empty, wait a few seconds, in which time a reload animation will be played(not there yet, but in the future this will be the case) then subtract the number of bullets that can fit in the magazine from the bullets in reserve.

What happens now is the current clip is emptied, it refills instantly, but there are no bullets subtracted from the reserve, and the canFire variable is never re-set to true. I need to know why canFire remains false, how to set the reload time as a public variable, and how to get the clip size to be subtracted from the reserve bullets. Thanks for your time!

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 yashpal · Nov 26, 2014 at 04:53 AM

Hello @caleb_b ,

I didn't tested it but might works

I find two problem here

(1) use StartCoroutine("Reload") in place of Reload();

(2)

  if(ammo <= 0) 
  {
      if(bulletsRemaining >= ammo)  
      {
          StartCoroutine("Reload");
          //bulletsRemaining -= ammo; // Here is ammo is always 0(zero) because of if(ammo <= 0). so use 
          if(bulletsRemaining >= ammoCapacity)
          {
             bulletsRemaining -= ammoCapacity;
             ammo = ammoCapacity;
          }
          else
          {
             ammo = bulletsRemaining;
             bulletsRemaining = 0;
          }
 
      }
  }

Hope it helps you.

Comment
Add comment · Show 2 · 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 caleb_b · Nov 26, 2014 at 05:03 AM 0
Share

Thanks @yashpal that fixed it. Works fine now. Id still like an aswer to my other question though. How can I set the reload time (the WaitForSeconds in Reload() ) as a public variable?

avatar image yashpal · Nov 26, 2014 at 05:40 AM 0
Share

@caleb_b, Yes you can make reloadTime as public variable.

 public float Gun1ReloadTime = 5f;
 public float Gun2ReloadTime = 6.3f;
 
 IEnumerator Reload(float TimeToReload) 
 {
     yield return new WaitForSeconds (TimeToReload);
         canFire = true;
 }

And Use

 StartCoroutine ("Reload", Gun1ReloadTime);

or

 StartCoroutine ("Reload", Gun2ReloadTime);


And for animation length use animation.clip.length.

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

27 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

Related Questions

Problem with animations(noob question i guess) 1 Answer

Renderer on object disabled after level reload 1 Answer

Multiple Cars not working 1 Answer

Reload clips on Gun-script not applied properly 1 Answer

Distribute terrain in zones 3 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