Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 FoundationGaming · Oct 14, 2021 at 09:18 PM · scalevariablesfloatrangepercentage

Scaled Variables

Hi everyone,

I have a question about scaling variables to one and other.

If I was to have a set of variables inside of a scriptable object ie:

 [Range(0,1)]
 float a;
 [Range(0,1)]
 float b;
 [Range(0,1)]
 float c;
 [Range(0,1)]
 float d;

and so on, is there a way I can link them together so that the combined variables are used as a percentage of each out of a whole. So I could have all 5 as 0.2 and then adjust one to be 0.4 and the others would drop to 0.15 automatically.

Is this possible or is there a better option that someone else can see that I cannot.

Thank you!

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 FoundationGaming · Oct 15, 2021 at 01:32 AM

I've had to fully create a custom method and change the entire process. Will make things a lot more difficult further down the line, but it's the only way to keep going for now. Thank you for the input!

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 Pangamini · Oct 15, 2021 at 08:48 AM 0
Share

It would be nice of you to share some of your results with others that come seek the same answer

avatar image FoundationGaming · Oct 15, 2021 at 01:14 PM 1
Share

I ended up using a completely different method, completely dumping the idea of the variables being connected. If I get it working how I wanted originally then I will be sharing the results for sure.

avatar image
2

Answer by Pangamini · Oct 14, 2021 at 10:36 PM

Implement the OnValidate() method. It gets called whenever you change the values in the inspector. You can normalize the values there. If all you want is to have the sum of them all to be 1, you could try

 protected void OnValidate()
 {
     float sum = a + b+ c + d;
     a /= sum;
     b /= sum;
     c /= sum;
     d /= sum;
 }

This might not be perfect, because it will also keep changing the value you are trying to set. You'd want to detect which one it is, and keep it out of the validation. I don't think you can detect that easily from OnValidate, but you could write a custom Editor / PropertyDrawer

Comment
Add comment · Show 3 · 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 Pangamini · Oct 14, 2021 at 10:37 PM 0
Share

Also, make sure the sum is never zero, or you will pierce the spacetime and go places

avatar image FoundationGaming · Oct 15, 2021 at 02:27 PM 0
Share

Unfortunately that only resulted in an "NaN" on all of the variables.

My code:

 [Range(0, 1)]
 public float percentIron = 1f; // To prevent dividing by ZERO
 [Range(0, 1)]
 public float percentCopper;
 [Range(0, 1)]
 public float percentCoal;
 [Range(0, 1)]
 public float percentGold;
 [Range(0, 1)]
 public float percentSilver;
 [Range(0, 1)]
 public float percentBauxite;
 [Range(0, 1)]
 public float percentSulfur;
 [Range(0, 1)]
 public float percentDiamond;
 [Range(0, 1)]
 public float percentTin;
 [Range(0, 1)]
 public float percent$$anonymous$$;
 
 protected void OnValidate()
 {
     float sum = percentIron + percentCopper + percentCoal +percentGold + percentSilver +percentBauxite + percentSulfur + percentDiamond + percentTin + percent$$anonymous$$;
     percentIron /= sum;
     percentCopper /= sum;
     percentCoal /= sum;
     percentGold /= sum;
     percentSilver /= sum;
     percentBauxite /= sum;
     percentSulfur /= sum;
     percentDiamond /= sum;
     percentTin /= sum;
     percent$$anonymous$$ /= sum;
 }

And I receive this: alt text

screenshot-2021-10-15-104844.png (16.1 kB)
avatar image Pangamini FoundationGaming · Oct 15, 2021 at 03:04 PM 0
Share

Well, your sum was obviously zero at some point ;) I don't see how that initial value prevents anything

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

132 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Floats sharing a range? 1 Answer

How to make Vector3s communicate with Floats? 2 Answers

Major int interacting with lesser float 1 Answer

Check increasing float variable 1 Answer

Xf notation vs just a number? 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