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 Flisijn · Mar 21, 2013 at 10:08 PM · variabledeclarepercent

How can I declare a variable as the 100% of another variable?

How can I declare a variable as the 100% of another variable?

Example:

var amount : int = 30; var percent : int = 100;

Percent variable must be the same as the amount variable.

Example:

percent = percent / 2; (30:2=15, so percent must be 15 now)

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 Bunny83 · Mar 21, 2013 at 10:43 PM 0
Share

I don't get this question, sorry... Your example doesn't explain what you want to do. Dividing 30 by 2 gives 15, yes .... So what do you actually want? o.O'

avatar image xt-xylophone · Mar 21, 2013 at 10:51 PM 2
Share

So you want a variable to be a percentage of another variable, and the percentage value is a range from 0-100?

The new number will be = amount*(percent/100)

So in your case: 30*(100/100) = 30, which is 100% of your amount(30)

Bunny83 is right, kind of weird wording....

It make be easier to understand if you store your 'percentage' value as a float or something from 0-1 and you can just do the multiplication rather than dividing it every time

avatar image Lockstep · Mar 21, 2013 at 11:01 PM 0
Share

Are you looking for a pointer? I don't about the js syntax though.

avatar image liamcary · Mar 22, 2013 at 05:40 AM 0
Share

in C# you could use a property. eg:

 public class ExampleClass : $$anonymous$$onoBehaviour
 {
     public const int fullAmount = 30;
 
     private float m_amount;
     public float Amount
     {
         get { return m_amount; }
         set 
         {
             m_amount = value;
             Percent = (m_amount / fullAmount) * 100; // "* 100" to get to your units
         }
     }
 
     private float m_percent;
     public float Percent
     {
         get { return m_percent; }
         set 
         {
             m_percent = value;
             Amount = fullAmount * m_percent / 100;
         }
     }
 }

That way when you change either one, the other will update to the correct relative value. You could also add conditions in the Percent's "set' function if you want to limit the percentage from 0 to 100 or something like that.

I'm not sure whether JS has an equivalent to a property, but if not you could just have a SetPercent() function and a GetPercent() function, etc. C# just does it more aesthetically.

avatar image whydoidoit · Mar 22, 2013 at 08:04 AM 0
Share

@liamcary - you've got an infinite recursion in that code...

I think you meant:

 public class ExampleClass : $$anonymous$$onoBehaviour
 {
     public const int fullAmount = 30;
  
     private float m_amount;
     public float Amount
     {
         get { return m_amount; }
         set 
         {
             m_amount = value;
             m_percent = (m_amount / fullAmount) * 100; // "* 100" to get to your units
         }
     }
  
     private float m_percent;
     public float Percent
     {
         get { return m_percent; }
         set 
         {
             m_percent = value;
             m_amount = fullAmount * m_percent / 100;
         }
     }
 }
Show more comments

0 Replies

· Add your reply
  • Sort: 

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

16 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

Related Questions

convert variables to C#? 1 Answer

Redim a variable 3 Answers

C# Custom Class - Declare Variable's Value 1 Answer

C#: Declaring a script as a variable 1 Answer

Can't use declared variable to find nearest Object 1 Answer


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