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 Diekeke · Oct 24, 2012 at 07:50 PM · valuelimitvarlifehealth

How do I set a max limit for my health value?

In the game I'm working on, the Main Character can pick up food to recover health (each piece recovers 10 points). I want the max value to be 100, but if I keep picking up food the script adds 10 points to the current value, and I end up having 110, 120, 130 points, etc. I want a script to limit the value so it can't be higher than 100 points.

I've been working on it but I can't seem to find a simple solution; is there an easy way to set a max limit so the points won't be higher than 100?

Thanks.

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

4 Replies

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

Answer by smirlianos · Oct 24, 2012 at 08:23 PM

 Var health : int = 100;
 
 Function update ()
 {
    If(health > 100)
    {
       health = 100;
    }
 }

Comment
Add comment · Show 4 · 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 smirlianos · Oct 24, 2012 at 08:15 PM 0
Share

Now when your health try to be bigger than 100, it automaticly turns into 100

avatar image Diekeke · Oct 24, 2012 at 09:05 PM 0
Share

Thanks man! :)

avatar image smirlianos · Oct 25, 2012 at 07:02 PM 0
Share

you're welcome :)

avatar image Memige · Oct 25, 2012 at 07:51 PM 1
Share

A good addition to this for future expand-ability would be to replace that 100 with another variable ala: var max_health : int = 100; then check if (health > max_health) health = max_health; this reads nicely and allows you to easily use the same value in other places as needed, and makes it clean and simple to change it if you need to.

avatar image
0

Answer by FuzzieLogick · Oct 24, 2012 at 09:07 PM

I'm pretty new to Unity myself but I think this may be able to be solved with a simple if statement. For example:

if(healthPoints >= 100){

healthPoints = 100;

}

You could put this in the Update() function so that it checks on every frame. If I'm right, this should prevent healthPoints from exceeding 100 by setting the value to 100 every time it exceeds that number. Oh, and this is in Javascript :)

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 Rati · Oct 24, 2012 at 09:07 PM

or you can check your life only when you pick up the food c# :

 void PickUpFood () 
 {
    health = health + 30;
 
    if(health > 100)
    {
       health = 100;
    }
 }
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 Thakazax · Mar 30, 2013 at 05:22 PM

You can make it shorter since it's only one line under the if statement.

 var health = 100;
 
 function Update ()
 {
    if(health > 100)
       health = 100;
 }
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

Time elapsed between variable change? 1 Answer

Destroy 1 prefab object and changing the color/image 1 Answer

Spinning Pointer that reacts when health drops 0 Answers

Health is set to zero at start of game. 3 Answers

Player Health Not Working? 4 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