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 RudeFX · Nov 17, 2012 at 01:57 AM · clamp

Counter issues, hitting -1?

Hello there all,

I've been having this problem with my counter, although I have wrote :

     if(units <=0)
     {
     units = 0;
     }
     if(units >=99)
     {
     units = 99;
     }

When manipulating it through code, it hits -1, then goes to 0. That quick hit is causing havock with my numbers, adding and extra unit where there should not be one.

Can someone give me a hand please?

Many thanks,

Keep it up guys!

Comment
Add comment · Show 4
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 dangermousenz · Nov 17, 2012 at 02:07 AM 0
Share

Well, a bit more information is needed on what is increasing or decreasing these numbers.

I'm looking at that and thinking that somehow too many "units" get removed, but it is some time before the code above runs, and corrects the mistake.

I would also be interested to know why you can't stop it beco$$anonymous$$g -1 in the first place. When you are subtracting one, shouldn't you first check that there are more than zero units?

avatar image Kale · Nov 17, 2012 at 02:23 AM 0
Share

Is it possible to get those if statements directly after your decrementing statements? It's hard to tell what's going on with just your if statements.

But it seems there is something in between your decrementing and the correction. Can you verify?

avatar image RudeFX · Nov 17, 2012 at 07:14 AM -1
Share

thanks for the quick response!

2nd time ive wrote this, did not post the first....

Its hard for me to explain, Im building an inventory in where you can craft items together. So I roam the world collecting materials, for instance "wood", I get it from an object by using:

inventory_Items.wood++;.

Then I go to my Inventory screen, where when I press on the wood button and I get an image of wood and a + & - next to it, so that I can add wood in quantities. So when I hit the + the units in the hud script go up, and the wood goes down in the inventory_Items script, thats all good, but when I press - it goes down to 0, then if I press again, it hits -1 then 0, but it gives me one of wood when doing it.

I am very greatfull fro all your anwsers, and sorry not expressing myself very clearly.

$$anonymous$$ale - I'm sorry I did not quite get your anwser, thanks anyway.

dangermouse - I do check to see if there is more than zero and Im doubble checking all my scripts to see, but it looks ok...Thanks a bunch also.

Erich5h5 - when I read mathclamp I was leaping for joy! I thought, thats it!, and know although its not visibly going under zero(stays at zero), if for instance I had 1 wood but presssed the - button twice (going below zero once, althought its not visible), and then kept pressed the + again, I would have 2 wood...

Thanks yet again all,

Patience is indeed a virtue,

Cheers!

avatar image dangermousenz · Nov 17, 2012 at 08:14 AM 0
Share

Can you give us the exact code that runs when you press the - button in your inventory? That would really help. and if that runs any other functions, possibly that too.

What you are doing sounds right, it should work, if the code makes sense.

1 Reply

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

Answer by Eric5h5 · Nov 17, 2012 at 02:34 AM

There's nothing in the code you posted that would cause any issues, but:

 units = Mathf.Clamp (units, 0, 99);
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

11 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

Related Questions

How to get RenderTexture non-square? 2 Answers

door script problems 2 Answers

Variable Mathf.Clamp not working... (SOLVED) 1 Answer

Is anyone able to fix my code? 3 Answers

What's the difference between clamp and repeat textures? 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