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 RafaelSantos · Jan 06, 2014 at 10:56 AM · iffor

Advice to make a lots of IF statements into a single FOR statement.

Hello guys. This is my first question in UnityAnswers :).I'm a beginner with programming but i'm trying to improve it.

I gonna show the code(JavaScript) and then I make my question.

         if(player1.transform.position == positions[0])
         {
             
             Debug.Log("Player1 esta no Inicio.");
             Turn.player1CaiuNaCasa = false;
         }
         
         if(player1.transform.position == positions[1])
         {    
             
             Debug.Log("Player1 esta na casa 1.");
             Turn.player1CaiuNaCasa = false;
         }
         
         if(player1.transform.position == positions[2])
         {
             
             Debug.Log("Player1 esta na casa 2.");
             Turn.player1CaiuNaCasa = false;
         }
         
         if(player1.transform.position == positions[3])
         {
         
             Debug.Log("Player1 esta na casa 3.");
             Turn.player1CaiuNaCasa = false;
         }
         
         if(player1.transform.position == positions[4])
         {
         
             Debug.Log("Player1 esta na casa 4.");
             Turn.player1CaiuNaCasa = false;
         }
         
         if(player1.transform.position == positions[5])
         {
         
             Debug.Log("Player1 esta na casa 5.");
             Turn.player1CaiuNaCasa = false;
         }
         
         if(player1.transform.position == positions[6])
         {
         
             Debug.Log("Player1 esta na casa 6.");
             Turn.player1CaiuNaCasa = false;
         }
         
         if(player1.transform.position == positions[7])
         {
             Debug.Log("Player1 esta na casa 7.");
             Turn.player1CaiuNaCasa = false;
         }

I have over 30 if statements like the one above. I wanna know if there is any possibility to make a for statement to do the same thing for all my ifs? Or any other thing to make it smaller.

Thanks all for helping me. Sorry about my english, I'm brasilian.

Comment
Add comment · Show 3
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 fafase · Jan 06, 2014 at 11:10 AM 1
Share

This will probably never work because of float precision. YOu'd rather clamp the value if you want to see it happening.

avatar image RafaelSantos · Jan 06, 2014 at 11:31 AM 0
Share

I'm not using float, I'm using int.

avatar image GameVortex · Jan 06, 2014 at 11:42 AM 1
Share

Even if you are using Int, Unity is still using float for the transform.position, which has floating point precision and may not be guaranteed to be a whole number. Just keep it in $$anonymous$$d.

2 Replies

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

Answer by GameVortex · Jan 06, 2014 at 11:05 AM

That is fairly easy because you are using the same objects and doing the exact same thing in all the if statements. The for loop could look something like this:

 for(int i = 0; i < positions.Length; i++)
 {
    if(player1.transform.position == positions[i])
    {
      Debug.Log("Player1 esta na casa " + i);
      Turn.player1CaiuNaCasa = false;
    }
 }

We use the positions.Length as the value that determines how many times the loop should run. If you want it to run a specified number of times instead of the length of the array, you can put that specific amount there instead of positions.Length.

Make sure you notice that we used the counter value 'i' to get the value from the array. It is also in the Debug.Log to show the correct value.

A final note is that it is usually a bad idea to compare positions directly because of how Unity handles position and float values, it is unlikely that the values will be the same. This of course depends on the rest of your implementation and how you want it to work.

Comment
Add comment · Show 1 · 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 RafaelSantos · Jan 06, 2014 at 11:16 AM 0
Share

Thanks a lot guys, GameVortex and flamy. You guys helped me a lot and saved me a bunch of space hahahaha.

avatar image
3

Answer by flamy · Jan 06, 2014 at 11:06 AM

Yes it is possible and that is how it should be done.

Look in to examples of how For works. Check this and this link.

In your case this will be the following.

 for (int i=0;i<positions.Length;i++)
 { 
    if(player1.transform.position == positions[i])
    {
        Debug.Log("Player1 esta na casa "+i+".");
        Turn.player1CaiuNaCasa = false;   
    }
 }

or you can use foreach if you dont need the index of wat position is...

 foreach (Vector3 position in positions)
 { 
    if(player1.transform.position == position)
    {
        //Do something for position....
        Turn.player1CaiuNaCasa = false;   
    }
 }



BTW when ever you use multiple if statements make sure that you are using a if...else

    if(player1.transform.position == positions[0])
    {
 
      Debug.Log("Player1 esta no Inicio.");
      Turn.player1CaiuNaCasa = false;
    }
 
    if(player1.transform.position == positions[1])
    { 
 
      Debug.Log("Player1 esta na casa 1.");
      Turn.player1CaiuNaCasa = false;
    }

the above one is less efficient than

    if(player1.transform.position == positions[0])
    {
 
      Debug.Log("Player1 esta no Inicio.");
      Turn.player1CaiuNaCasa = false;
    }
    else if(player1.transform.position == positions[1])
    { 
 
      Debug.Log("Player1 esta na casa 1.");
      Turn.player1CaiuNaCasa = false;
    }


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

21 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

Related Questions

Check for collision in for loop c# 1 Answer

For loop has 'if-else' impossibility. 2 Answers

Exiting an if statement when condition has been met 1 Answer

Read Array Inside Array 0 Answers

How do I check if all objects in an array are destroyed? 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