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 joreng26 · Mar 15, 2014 at 10:37 PM · javascriptguishootingcounter

ammo counter, counts wrong

Hi guys! i have an error with my ammo count, it shows up on the screen and everything but it substracts two instead of one each time. If i remove the "static" var it counts one and one. but with a static var (its needed) it counts double, help?

-thanks in advance

 #pragma strict
 
 var muzzleFlash : Renderer;
 var muzzleLight : Light;
 
 var reload = true;
 static var Ammo = 31;
 
 function Start () {
 
 muzzleFlash.enabled = false;
 muzzleLight.enabled = false;
 
 }
 
 function Update () {
   if(Ammo >= 0){
    if(Input.GetButtonDown("Fire1")) {
      Shoot();
   }
  }
 }
 function Shoot ()
 {
   muzzleFlash.renderer.enabled = true;
   muzzleLight.enabled = true;
   yield WaitForSeconds(0.02);
   Ammo -= 1;
   muzzleFlash.renderer.enabled = false;
   muzzleLight.enabled = false;
   audio.Play();
  
 }
 function OnGUI (){
 GUI.Label (Rect (Screen.width/4+Screen.width/4+Screen.width/4+Screen.width/8,Screen.height/4+Screen.height/4+Screen.height/4+Screen.height/8,50,30), Ammo.ToString());
 }
Comment
Add comment · Show 2
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 Jeremy2300 · Mar 16, 2014 at 01:32 AM 0
Share

I wonder if it's just how it's getting called.

$$anonymous$$aybe include an isFiring boolean in your code, that will only call Shoot() when false, when you go through the Shoot function set it to true before the yield and false afterwards.

What might be happening is it doesn't actually wait 0.02 before firing again.

avatar image Sildaekar · Mar 16, 2014 at 01:57 AM 0
Share

I'm going to have to go with what Jeremy2300 said, you may want to add a bool in there to make sure it's only firing when it's suppose to.

But I did find something else that may cause a problem. this: if(Ammo >= 0){ should probably just be "less than" ins$$anonymous$$d of "less than or equal to". With the way it is right now you will be able to fire when your ammo reaches 0. (Unless you are accounting for one in the chamber and this is only for the magazine)

1 Reply

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

Answer by swalex · Mar 16, 2014 at 01:58 AM

Do you probably have two instances of your ammo counter behaviour in your scene (for a second player probably)? They would both call Shoot() on 'Fire1' and they would both decrease Ammo by 1. If it is a non-static field, each behaviour instance would decrease its own ammo counter, but in your case they would both decrease the same counter.

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 Sildaekar · Mar 16, 2014 at 02:00 AM 0
Share

Good point, I didn't think about that one...but that's probably the issue with his counter.

avatar image joreng26 · Mar 16, 2014 at 11:50 AM 0
Share

Thanks alot, i didnt think about that :)

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

23 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

Related Questions

Setting Scroll View Width GUILayout 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Limit on GUI Components? 0 Answers

Rapid fire Help 1 Answer

Android touch gui help!!! 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