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 /
  • Help Room /
avatar image
0
Question by Miso-c9 · Jan 27, 2018 at 09:35 AM · errorinstantiategunienumeratorreloading

My gun reloading dont work,can i please get help.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class shoot : MonoBehaviour {
     
     public Rigidbody  bulletPrefab;
     public Transform  bulletSpawn;
     public float shootingSpeed;
     public int bulletAmmount;
     int shootedBulletAmmount;
     public int currentBulletAmmount;
 
     bool isSpawned;
     bool canShoot;
 
     void Start()
     {
         canShoot = true;
     }
 
     void Update()
     {
         Shoot ();
         Reload ();
 
     }
 
     void Shoot ()
     {
         if(canShoot==true)
         
         {
             if (Input.GetButtonDown ("Fire1")) {
                 
                 shootedBulletAmmount = shootedBulletAmmount++;        
                 isSpawned = true;
                 print (shootedBulletAmmount);            
                 Rigidbody rocketInstance;
                 rocketInstance = Instantiate (bulletPrefab, bulletSpawn.position,  bulletSpawn.rotation) as Rigidbody;
                 rocketInstance.AddForce (bulletSpawn.forward * shootingSpeed);
         
         }
     }
     
 }
 
     void Reload()
 
     {
         if(isSpawned==true)
         {
             currentBulletAmmount = bulletAmmount -= shootedBulletAmmount;
 
         }
 
         if (currentBulletAmmount <= 0) 
         {
             canShoot = false;
         }
 
         if (canShoot == false) 
         {
             StartCoroutine("readyShoot");
         }
    }
 
           
         
 
     IEnumerator readyShoot()
     {
         yield return new WaitForSeconds (3);
         canShoot = true;
         bulletAmmount = 20;
         shootedBulletAmmount = 0;
         currentBulletAmmount = 0;
 
     }
 
 
 
 
 }
 

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by LTonon · Jan 27, 2018 at 11:10 AM

Ok, so first of all it seems like your variable "isSpawned" doesn't actually do anything, since it'll always be "true" after the first shot... You could get rid of the first if statement on the Reload function and use simply like this:

      void Reload()
  
      {

          currentBulletAmmount = bulletAmmount -= shootedBulletAmmount;

  
          if (currentBulletAmmount <= 0) 
          {
              canShoot = false;
          }
  
          if (canShoot == false) 
          {
              StartCoroutine("readyShoot");
          }
     }


And I guess that your problem is on how you are doing the reload, since calling the function on the Update() all of the time should make you reload super fast and so it would not decrease your current number of bullets (I recommend associate a button press to it or reloading only when there are no more bullets)... Right? What exactly is happening?


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 Miso-c9 · Jan 27, 2018 at 03:37 PM 0
Share

first of all thanks. now just dont want to count numbers and both variables are keeped on 20 what should i do

avatar image LTonon Miso-c9 · Jan 27, 2018 at 04:44 PM 0
Share

What do you mean by "don't want to count numbers"? And did you change something on your script? Try this and tell me what happens, I tried to clean up your code a little to make easier to understand. I guess that you have an error on the script because you are using "if (isSpawned == true) { currentBulletAmount = bulletAmmount -= shootedBulletAmmount; }", but since you never change "isSpawned" to false, this is always called... And right after a reload, "currentBulletAmount = 0", meaning that it'll always reload after the first time it has reloaded.

 void Reload() 
 {
     if (bulletAmount - shootedBulletAmount <= 0) {
         canShoot = false;
         StartCoroutine("readyShoot");
     }
 }
 
 IEnumerator readyShoot() 
 {
     yield return new WaitForSeconds(3f);
     shootedBulletAmount = 0;
     canShoot = true;
 }    
 
avatar image
0

Answer by Miso-c9 · Jan 27, 2018 at 05:52 PM

oke i tried some different and it works.. btw thx for help and waste ur time, but u helpeded me to learn some

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

98 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

Related Questions

Need help with shooting and reload script 1 Answer

Instantiate not repeating? 1 Answer

How Can i run a method afterequal integer number counts using Ienumerator ?? 0 Answers

Help with my Arrow Fire Script 1 Answer

Gun not reloading properly & time not deducting correctly when below 10 seconds 2 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