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 /
avatar image
0
Question by srafciger · Jul 07, 2011 at 04:06 PM · intrange

Number Range

How do I create range of numbers(int) from min to max ?.

I want min to be 0 and max to be 100.

I want to check if number is in this range and prevent it from leaving that range.

Here's the code:

     var Skin : GUISkin;
 static var att = (Mathf.Clamp(50, 0, 100));
 static var bck = (Mathf.Clamp(50, 0, 100));
 static var rem = (Mathf.Clamp(25, 0, 100));
 var btnTextureRight : Texture;
 var btnTextureLeft : Texture;
 
 function OnGUI () {
 
 if (GUI.Button(Rect(100,200,25,25),btnTextureRight)){
 att++;
 rem--;
 }
 if (GUI.Button(Rect(10,200,25,25),btnTextureLeft)){
 att--;
 rem++;
 }
 if (GUI.Button(Rect(100,250,25,25),btnTextureRight)){
 bck++;
 rem--;
 }
 if (GUI.Button(Rect(10,250,25,25),btnTextureLeft)){
 bck--;
 rem++;
 }
 GUI.Label(Rect(60,200,90,50),att.ToString());
 GUI.Label(Rect(60,250,90,50),bck.ToString());
 GUI.Label(Rect(60,300,90,50),rem.ToString());
 }


But number is crossing my range.

Any idea how to fix this problem?

Comment
Add comment · Show 7
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 dibonaj · Jul 07, 2011 at 04:13 PM 0
Share

Can you elaborate on what you are trying to do? Are you trying to create a function that goes from 25 to whatever, or just check to make sure that a number falls in a certain range?

avatar image flaviusxvii · Jul 07, 2011 at 04:17 PM 0
Share

You took the time to write a question and then didn't bother to make sure it was clear.

Give this a read: http://answers.unity3d.com/questions/133869/how-to-ask-a-good-question.html

avatar image srafciger · Jul 07, 2011 at 04:28 PM 0
Share

dibonaj, I want to check if number falls in a certain range (from 0 to 100).

avatar image Tasarran · Jul 07, 2011 at 04:35 PM 0
Share

This is very confusing...

Do you want to create a random number, like you say in your question, or check a value, like you say in your comment?

Very unclear.

avatar image srafciger · Jul 07, 2011 at 04:37 PM 0
Share

I said in question that I don't want Random.Range. I want to check if number is in range from 0 to 100, and prevent it from leaving that range

Show more comments

3 Replies

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

Answer by flaviusxvii · Jul 07, 2011 at 04:41 PM

It sounds like you are looking for Mathf.Clamp.

http://unity3d.com/support/documentation/ScriptReference/Mathf.Clamp.html (The second one is for ints)

 att = Mathf.Clamp(att + 1, 0, 100);
 rem = Mathf.Clamp(rem - 1, 0, 100);
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 srafciger · Jul 07, 2011 at 04:44 PM 0
Share

Can you please check my code and write me where did I make the mistake?

avatar image flaviusxvii · Jul 07, 2011 at 04:44 PM 0
Share

I edited my answer with an example of what you need to do..

avatar image
0

Answer by deeredman1991 · Jul 07, 2011 at 04:22 PM

couldn't you use an array to store multiple values in a specific range?

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 Sebas · Jul 07, 2011 at 04:29 PM

Have you tried Mathf.Clamp (might have to change to int) or possibly just use Random.Range and add 25 or whatever you want your starting number to be. Though I'm not exactly sure whether that's what you're looking for.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Mathf.Clamp an int value 4 Answers

Does anyone know why this is wrong? 2 Answers

Making a plain number range 2 Answers

Script so that, my enemies only see my player at a certain range. 4 Answers

Damage Within Range 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