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 gian_unity190 · Mar 01, 2020 at 08:47 PM · if-statementsmodularhealth

Inverse Modulo?

     if (Input.GetKeyDown("q"))
     {
         int i = 0;
         while (!Players[i].GetComponent<PlayerSelectionCheck>().Active && (i < Players.Count))
         { i++; }
         Players[i].GetComponent<PlayerSelectionCheck>().Active = false;
         Players[i].GetComponent<PlayerSelectionCheck>().OneTime = false;
         //if active
         Players[(i + 1) % Players.Count].GetComponent<PlayerSelectionCheck>().Active = true;
         Players[(i + 1) % Players.Count].GetComponent<PlayerSelectionCheck>().OneTime = false;
     }

in this Script it turns off Active for the current active object and turns it on for the next one in the list.

     if (Input.GetKeyDown("e"))
     {
         int i = 0;
         while (!Players[i].GetComponent<PlayerSelectionCheck>().Active && (i < Players.Count))
         { i++; }
         Players[i].GetComponent<PlayerSelectionCheck>().Active = false;
         Players[i].GetComponent<PlayerSelectionCheck>().OneTime = false;
         //if active
         Players[(i - 1) % Players.Count].GetComponent<PlayerSelectionCheck>().Active = true;
         Players[(i - 1) % Players.Count].GetComponent<PlayerSelectionCheck>().OneTime = false;
     }

this script should do the same but in the inverted direction but the modulo does not work with - numbers. so i made this script:

     if (Input.GetKeyDown("e"))
     {
         int i = 0;
         while (!Players[i].GetComponent<PlayerSelectionCheck>().Active && (i < Players.Count))
         { i++; }
         Players[i].GetComponent<PlayerSelectionCheck>().Active = false;
         Players[i].GetComponent<PlayerSelectionCheck>().OneTime = false;
         //if inactive
         i--;
         if (i < 0){i = Players.Count;}
         Players[i].GetComponent<PlayerSelectionCheck>().Active = true;
         Players[i].GetComponent<PlayerSelectionCheck>().OneTime = false;
     }

but for some reason this still does not work... can someone tell me where the mistake is?

Comment
Add comment · Show 2
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 gian_unity190 · Mar 01, 2020 at 03:52 PM 0
Share

the mistake was not with the modulo. id had to do with Players.Count it is the number of objects in player list counted from 1 and not 0 (so just Player.Count-1)

avatar image Bunny83 gian_unity190 · Mar 01, 2020 at 11:53 PM 0
Share

No, that doesn't make much sense. You have to use Player.Count as divisor. The remainder is always smaller than the divisor. So for example if the count is 9 the remainder can never be larger than 8. That should make sense since a value of 9 would be divisible by 9 with 0 remainder. So using (Player.Count-1) as divisor makes no sense and won't solve the -1 issue.

1 Reply

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

Answer by Bunny83 · Mar 02, 2020 at 12:33 AM

Your issue is that the modulo operator will keep the sign of the number you're dividing, This is explained in the documentation. So if you have a value smaller than 0 (namely "-1" the modulo operator will return "-1" and not the divisor-1).


Your last code does actually work, however the value you set inside your if statement is wrong. The highest valid index is Player.Count - 1.


ps: Now I maybe understand what you meant by your comment above, however it's misleading the way it's worded. The modulo operator doesn't work for negative numbers since it will return just the negative modulo. A common solution is to just "add" Player.Count to your modulo result if it's negative. So if the result is "-1" and you add Player.Count you actually get "Player.Count - 1". Of course since we need an if statement anyways we don't need to do the modulo since you always just decrement by 1. However a more general solution is this:

 int w = i % Player.Count;
 if (w < 0)
     w += Player.Count;

This should works properly for any integer number i and it will be wrapped properly on both ends.

Some examples with a Count of "5":

 //   i   | i%c |   w   | c 
 //-------|-----|-------|---
 //  11   |  1  |   1   | 5 
 //   3   |  3  |   3   | 5 
 //   2   |  2  |   2   | 5 
 //   1   |  1  |   1   | 5 
 //   0   |  0  |   0   | 5 
 //  -1   | -1  |   4   | 5 
 //  -2   | -2  |   3   | 5 
 //  -3   | -3  |   2   | 5 
 //  -4   | -4  |   1   | 5 
 //  -5   |  0  |   0   | 5 
 //  -6   | -1  |   4   | 5 
 //  -7   | -2  |   3   | 5 

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 gian_unity190 · Mar 02, 2020 at 07:13 PM 0
Share

thanks for your explanation this will help for future scripts. but the way I did it works flawlessly for the purpose I'm using it for so I don't really care ;D.

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

123 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

Related Questions

keycode and && statment not working? 1 Answer

How To Make Player Health Increase if in a Certain Animation State 1 Answer

Combining if Statements 1 Answer

Single line if statement with many conditions or several single-conditioned if statements 1 Answer

C# the position of the 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