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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by FutureTech_Coker · Apr 16, 2015 at 01:57 AM · c#fpsscriptingbasics

Reload function in my script not working at all. No error messages in compiler.

 using UnityEngine;
 using System.Collections;
 
 public class shootScript : MonoBehaviour {
 
     public Rigidbody bullet; 
     private int throwPower = 200;
     public float fireRate = 0.15f; 
     private float nextFire = 0.0f;
     public float soundRate = 0.15f;
     private float nextSound = 0.0f; 
     private float flashRate = 0.15f;
     private float nextFlash = 0.0f; 
     public int clips = 10; 
     public int maxClip = 60; 
     public int ammoClip;  
     private int bulletsReq; 
     private float reloadTime = 1.6f; 
     public int totalBullets = 600;  
     public bool canFire = false; 
     public Animation gunRecoil; 
     public GameObject akShot; 
     public GameObject finalShot;
     public GameObject muzzleBlast;
 
      
 
     // Use this for initialization
     void Update () {
         bullet = bullet.GetComponent<Rigidbody> ();
 
         if (ammoClip > 0)
             canFire = true;
         if (ammoClip <= 0)
             canFire = false; 
         if (ammoClip > maxClip)
             ammoClip = maxClip; 
 
         if (Input.GetButton ("Fire1") && canFire == true)
             IsFiring ();  
         
         if (Input.GetButtonDown ("Fire1") && canFire == true) 
             IsFiring (); 
 
         if (Input.GetButtonDown ("Fire2") && ammoClip < maxClip) 
             Reload(); 
             canFire = false;
 
 
          
 
     }
     
     // Fires one shot each time the button is pressed. 
     void IsFiring () { 
 
         if (ammoClip > 0 && canFire == true) {
             GameObject muzzleFlash = Instantiate (muzzleBlast, this.transform.position, this.transform.rotation) as GameObject; 
             GameObject lastShot = Instantiate (finalShot, this.transform.position, this.transform.rotation) as GameObject; 
             Rigidbody clone = Instantiate (bullet, transform.position, transform.rotation) as Rigidbody;
             clone.velocity = transform.TransformDirection (Vector3.forward * throwPower); 
             ammoClip --;
         }
         else if (ammoClip > 0 && canFire == true && Time.time > nextFire && Time.time > nextSound && Time.time > nextFlash) {
             nextFire = Time.time + fireRate;
             nextSound = Time.time + soundRate;
             nextFlash = Time.time + flashRate; 
             Rigidbody clone = Instantiate (bullet, transform.position, transform.rotation) as Rigidbody;
             clone.velocity = transform.TransformDirection (Vector3.forward * throwPower);
             GameObject gunShot = Instantiate (akShot, this.transform.position, this.transform.rotation) as GameObject;
             GameObject muzzleFlash = Instantiate (muzzleBlast, this.transform.position, this.transform.rotation) as GameObject; 
             ammoClip --;
         if (ammoClip == 0)
                 canFire = false; 
                 StartCoroutine (Reload ());      
             }
     }
     IEnumerator Reload () { 
         if (ammoClip == 0)
             canFire = false;  
         if (Input.GetKeyDown (KeyCode.R) && ammoClip > 0)
                 canFire = false; 
         yield return new WaitForSeconds (reloadTime); 
                 ammoClip = maxClip;
                      
         }
 
     }

Comment
Add comment · Show 1
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 JustinC · Apr 16, 2015 at 03:41 AM 0
Share

try switching line 75 and 74 around, that might help. load the co-routine before the you switch the bool. not sure if that will help but worth a shot

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by ndrummer31 · Apr 16, 2015 at 05:23 AM

Hi FutureTech_Coker! There is no compiler error because technically there is no error. The "error" is very simple, and I noticed you did more than once in your code. If statements only work without curly brackets if there is ONE line of code within it.

What you wrote was:

 if (Input.GetButtonDown ("Fire2") && ammoClip < maxClip) 
      Reload(); 
      canFire = false;

So the compiler is really reading this:

 if (Input.GetButtonDown ("Fire2") && ammoClip < maxClip)
 {
      Reload();
 }
 canFire = false;

It should be:

 if (Input.GetButtonDown ("Fire2") && ammoClip < maxClip)
 {
      Reload(); 
      canFire = false;
 }

Another instance with the same error:

 if (ammoClip == 0)
      canFire = false; 
      StartCoroutine (Reload ());   

I hope this helps and as always, happy coding :)

~nDrummR

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 FutureTech_Coker · Apr 17, 2015 at 12:45 AM 0
Share

I thought that was gonna work drummer but for some reason it's still doing the same thing. Letting me play and fire but it won't let me reload.

avatar image
1

Answer by revolute · Apr 17, 2015 at 01:13 AM

Well, your branching is wrong.

Look at this :

 if (ammoClip > 0 && canFire == true){}
 
 else if (ammoClip > 0 && canFire == true && Time.time > nextFire && Time.time > nextSound && Time.time > nextFlash){}


You will never get into else if with this check.

If(ammoClip > 0 && canFire == true) is true, then 'if' is entered and not 'else if'. If(ammoClip > 0 && canFire == true) is false, then 'if' wont be entered but neither will it enter 'else if'.

Fix this part and it will work.

And FYI, IEnumerators only work when you call it with StartCoroutine. Sadly, your Reload() in line 46 will never be run, which may also be a part of your troubles.

Comment
Add comment · Show 10 · 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 FutureTech_Coker · Apr 19, 2015 at 05:41 PM 0
Share

So I would have to put StartCoroutine next to reload in the update function as well?

avatar image revolute · Apr 20, 2015 at 07:04 AM 0
Share

The full line would be StartCoroutine(Reload());.

avatar image revolute · Apr 20, 2015 at 07:09 AM 0
Share

Anyways, whenever you call a IEnumerator method, remember to use StartCoroutine as calling the method without it wont get it run.

And compiler wont throw any errors or warnings.

avatar image FutureTech_Coker · Apr 21, 2015 at 12:17 AM 0
Share

Thanks everyone you guys have all been pretty helpful, I finally got it working. Thanks Again.....

avatar image FutureTech_Coker · Apr 21, 2015 at 12:46 AM 0
Share

using UnityEngine; using System.Collections;

public class shootScript : $$anonymous$$onoBehaviour {

 public Rigidbody bullet; 
 private int throwPower = 200;
 public float fireRate = 0.15f; 
 private float nextFire = 0.0f;
 public float soundRate = 0.15f;
 private float nextSound = 0.0f; 
 private float flashRate = 0.15f;
 private float nextFlash = 0.0f; 
 public int clips = 10; 
 public int maxClip = 60; 
 public int ammoClip;  
 private int bulletsReq; 
 private float reloadTime = 1.6f; 
 public int totalBullets = 600;  
 public bool canFire = false; 
 public Animation gunRecoil; 
 public GameObject akShot; 
 public GameObject finalShot;
 public GameObject muzzleBlast;

  

 // Use this for initialization
 void Update () {
     bullet = bullet.GetComponent<Rigidbody> ();

     if (ammoClip > 0)
         canFire = true;
     if (ammoClip <= 0)
         canFire = false; 
     if (ammoClip > maxClip)
         ammoClip = maxClip; 

     if (Input.GetButton ("Fire1") && canFire == true)
         IsFiring ();  
     
     if (Input.GetButtonDown ("Fire1") && canFire == true) 
         IsFiring (); 

     if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.R) && canFire < maxClip)
     {
         StartCoroutine (Reload());
     
         canFire = false;
     }
      

 }
 
 // Fires one shot each time the button is pressed. 
 void IsFiring () { 

     if (ammoClip > 0 && canFire == true) {
         GameObject muzzleFlash = Instantiate (muzzleBlast, this.transform.position, this.transform.rotation) as GameObject; 
         GameObject lastShot = Instantiate (finalShot, this.transform.position, this.transform.rotation) as GameObject; 
         Rigidbody clone = Instantiate (bullet, transform.position, transform.rotation) as Rigidbody;
         clone.velocity = transform.TransformDirection (Vector3.forward * throwPower); 
         ammoClip --;
     } 
     else if (ammoClip > 0 && canFire == true && Time.time > nextFire && Time.time > nextSound && Time.time > nextFlash) {
         nextFire = Time.time + fireRate;
         nextSound = Time.time + soundRate;
         nextFlash = Time.time + flashRate; 
         Rigidbody clone = Instantiate (bullet, transform.position, transform.rotation) as Rigidbody;
         clone.velocity = transform.TransformDirection (Vector3.forward * throwPower);
         GameObject gunShot = Instantiate (akShot, this.transform.position, this.transform.rotation) as GameObject;
         GameObject muzzleFlash = Instantiate (muzzleBlast, this.transform.position, this.transform.rotation) as GameObject; 
         ammoClip --;

         if (ammoClip == 0) {
             Reload (); 
             canFire = false; 
         }
     }
 }
 IEnumerator Reload () {
     if (ammoClip == 0)
         canFire = false;  
     else if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.R) && ammoClip > 0) {
         yield return new WaitForSeconds (reloadTime); 
         canFire = false; 
          ammoClip += maxClip;
      }

 }

}

Show more comments
avatar image
0

Answer by lordlycastle · Apr 16, 2015 at 03:56 AM

Well of course it’s working, otherwise it’ll give compiler error. Try using the debugger, it’ll help you understand what’s happening. Put a breakpoint in Reload and see how it executes line by line. If you have Monodevelop here are the instructions. I’m sure you’ll find them for VS, just google it. Otherwise you can just use Debug.Log, the functionality is limited with it.

Comment
Add comment · 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
0

Answer by FutureTech_Coker · Apr 17, 2015 at 01:01 AM

Well I was certain your answer was going to work drummer, but for some silly reason it's still doing the exact same thing. It's letting me play and fire but it will not let me reload at all. I am lost on it. I want to be able to reload the difference needed to fill the clip when I hit R or fire2 ATM, when I have more then 0 ammo in my current ammoClip.

Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How do I make my bullet tracers show when shooting in the air? 0 Answers

JS and C# how to open in different programs. 1 Answer

Overwrite the value in my List but why? 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