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 dbknights2000 · Oct 04, 2015 at 03:34 AM · c#scripting probleminventory

How can I stop an int from adding with the getkeydown

When I collect a battery it goes into the int Battery Count. When the current battery power reaches 0 and the battery count is greater than 0 the battery count reduces and current battery power increases 100%. That works. But what I want is for when the current battery power reduces to 50% I want to press the "T" key and use one of the Battery Count and increase back to 100%. What happens is that I can continue press "T" and the count goes into a negative number and the Current battery power goes to 100% instead of ending at 0 battery counts and 0 % current battery Power. I know I need an ELSE in the script but there is where I get in trouble with it. Part of the script is below.

public float _currantBatteryPower = 0f; public static int BatteryCount; void Update(){

if (_currantBatteryPower <= 0 && BatteryCount > 0)
{
BatteryCount--;
_currantBatteryPower = 100;
} if (Input.GetKeyDown(KeyCode.T) && _currantBatteryPower <= 50 ) {

         GetComponent<AudioSource>().PlayOneShot(_PickupSound);
         BatteryCount--;
         _currantBatteryPower = 100;
     }
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 Suddoha · Oct 04, 2015 at 12:28 PM

You can either add another condition to the second if statement like

 if (Input.GetKeyDown(KeyCode.T) && _currantBatteryPower <= 50 && BatteryCount > 0)

or you avoid redundant code and do it like this, for example:

 public float _currentBatteryPower = 0f; 
 public static int BatteryCount; 

 void Update()
 {
     UseBattery(0f);
 
     if (Input.GetKeyDown(KeyCode.T))
         UseBattery(50f);
  }
 
  private void UseBattery(float threshold)
  {
      if (_currentBatteryPower <= threshold && BatteryCount > 0)
      {
          BatteryCount--;
          _currentBatteryPower = 100;
       }
   }

(i renamed currantBatteryPower to currentBatteryPower with e, either change it again or make sure the other scripts use the same name)

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 dbknights2000 · Oct 04, 2015 at 01:31 PM

Thank You, Thank You, this was driving me batty, and now I can relax and play Bat Man this fine Sunday Morning.

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

31 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

Related Questions

RPG Picking up items with polymorphism - probably quick :) 1 Answer

How to convert object types? 0 Answers

Best way to simplify this code 1 Answer

Inventory help 0 Answers

Trying to "condense" a lot of verbose code, for an inventory system. Don't know where to start. 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