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 ivan_hellgames · Aug 06, 2013 at 11:23 AM · variablefunctionglitchammoupdating

Glitchy variable when updating through a function

I have a simple shooting game with two gameobjects - the gun and the ammo controller. This is my script for the ammo controller:

 #pragma strict
 var ammo:int;
 var gun:GameObject;
 function Start () {
     ammo=100;
 }
 function Update() {
     Debug.Log(ammo);
 }
 function Shoot () {
     ammo-=1;
     gun.GetComponent(gun_script).ShootEffect();
 }

The function Shoot () is called by the gun when I press the left mouse button. The problem is that the ammo variable doesn't decrease with 1 after every time the function is called. I shoot like 10 times, wait some seconds and the variable starts to decrease very fast until it gets to 90 (some seconds after I finished shooting). However, if I put the ammo decreasing code in the Start or the Update function this bug doesn't appear. For example:

 function Start() {
     ammo=100;
     while(true) {
        ammo-=1;
        yield WaitForSeconds(0.5);
     }
 }

This time it works but I don't want to decrease the ammo every half a second. Only when the function is called. Can somebody help me?

Sorry for my bad English.

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

Answer by pheash · Aug 06, 2013 at 11:49 AM

Heya, Not quite sure if I am right, but to me it sounds like the problem might be in the way you call the shoot function. Using Input.GetMouseButtonDown is only called once whereas Input.GetMouseButton would call the function while the button is down, leading to it being called multiple times.. here is a link to the documentation http://docs.unity3d.com/Documentation/ScriptReference/Input.GetMouseButtonDown.html If that doesnt help could you paste the bit where you call the Shoot function.

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 ivan_hellgames · Aug 06, 2013 at 12:54 PM

I use GetMouseButton. Here is the code:

 if(Input.GetMouseButton(0)) {
     if(!mesh.animation.isPlaying && ammo_controller.GetComponent(ammo_script).ammo>0){
         mesh.animation.Play();
         MuzzleFlash();
         ammo_controller.GetComponent(ammo_script).Shoot();
     }
 }

It checks if the gun is not shooting (the animation is not playing) and if I have ammo. I tried with GetMouseButtonDown but the problem is the same.

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

15 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

Related Questions

Problem with increasing a variable 1 Answer

Writing PropertyDrawers for functions. 1 Answer

Lower player's health from separate script. 2 Answers

Accessing a variable within a function.... GetComponent() 2 Answers

Getting a Non Static Variable From a Different C# Script 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