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 El-Deiablo · Dec 13, 2016 at 08:14 PM · c#if-statementssimplify

Simplify Code

I have a random object with a random size that is chosen at the start. The selected size effects my height and width variables. My code does what I want, but there's a lot of if functions which is not pretty. I want to know if there is something I could look into to simplify this code into fewer lines and "if" functions.

Here's the code:

  if (PlanetSpawner.randomSize >= 0.5f && PlanetSpawner.randomSize < 0.6f) {
 
             width = 2.8f;
             height = 2.8f;
 
         }
 
         if (PlanetSpawner.randomSize >= 0.6f && PlanetSpawner.randomSize < 0.7f) {
 
             width = 3.0f;
             height = 3.0f;
 
         }
 
         if (PlanetSpawner.randomSize >= 0.7f && PlanetSpawner.randomSize < 0.8f) {
 
             width = 3.2f;
             height = 3.2f;
 
         }
 
         if (PlanetSpawner.randomSize >= 0.8f && PlanetSpawner.randomSize < 0.9f) {
 
             width = 3.4f;
             height = 3.4f;
 
         }
 
         if (PlanetSpawner.randomSize >= 0.9f && PlanetSpawner.randomSize < 1.0f) {
 
             width = 3.6f;
             height = 3.6f;
 
         }
 
         if (PlanetSpawner.randomSize >= 1.0f && PlanetSpawner.randomSize < 1.1f) {
 
             width = 3.8f;
             height = 3.8f;
 
         }
 
         if (PlanetSpawner.randomSize >= 1.1f && PlanetSpawner.randomSize < 1.2f) {
 
             width = 4.0f;
             height = 4.0f;
 
         }
 
         if (PlanetSpawner.randomSize >= 1.2f && PlanetSpawner.randomSize < 1.3f) {
 
             width = 4.2f;
             height = 4.2f;
 
         }
 
         if (PlanetSpawner.randomSize >= 1.3f && PlanetSpawner.randomSize < 1.4f) {
 
             width = 4.4f;
             height = 4.4f;
 
         }
 
         if (PlanetSpawner.randomSize >= 1.4f && PlanetSpawner.randomSize < 1.5f) {
 
             width = 4.6f;
             height = 4.6f;
 
         }
 
         if (PlanetSpawner.randomSize >= 1.5f && PlanetSpawner.randomSize < 1.6f) {
 
             width = 4.8f;
             height = 4.8f;
 
         }
Comment
Add comment · Show 1
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 tanoshimi · Dec 13, 2016 at 09:10 PM 0
Share

How is randomSize set? What do you want to happen if it's less than 0.5 or greater than 1.6? Do you ever need for width and height to be different from each other?

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Dec 13, 2016 at 09:22 PM

Just do this:

 if (PlanetSpawner.randomSize >= 0.5f && PlanetSpawner.randomSize < 1.6f)
 {
     float v = PlanetSpawner.randomSize - 0.5f; // offset by 0.5 so we start at "0".
     int i = Mathf.FloorToInt(v*10); // i will go up by one every "0.1"
     width = 2.8f + 0.2f * i; // for every "i" we add 0.2
     height = 2.8f + 0.2f * i;
 }

This will do exactly the same as all your if-statements.

So if "randomSize" is "1.55"
---(-0.5)---> 1.05
---FloorToInt(x*10)---> 10
---(*0.2)---> 2f
---> 2.8f + 2f ---> 4.8f

So you get the exact same result when the value is in between 1.5 and 1.6

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 UnityCoach · Dec 13, 2016 at 08:55 PM

This is a mutually exclusive case. Performance wise, you want to at least use "else if" as the value will never be within more than one range. You may also want to use a switch statement instead.

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 callme · Dec 13, 2016 at 08:50 PM

 float num = Random.randomRange(2.8f, 4.8f);
 transform.localScale = Vector.one * num;

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

257 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 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 Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Input.GetKey(KeyCode.E) requires multiple presses. 1 Answer

If statement behaves unexpectedly 3 Answers

How can i simplify multiple if-statements? 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