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 /
  • Help Room /
avatar image
0
Question by Wylie-Modro · Oct 05, 2016 at 02:22 AM · arraysfor-loopboolindexof

System.Array.IndexOf iterating when it should not

I have an Array of bools and when the function Key1Pushed() is called I want to iterate through the array setting them all to false, except for the bool key1 which I want to set to true.

I believe this code should work however whenever I call Key1Pushed() I encounter 2 issues:

  1. System.Array.IndexOf(allKeys, key1) returns 1, when i = 0; returns 2, when i=1, and so on until it returns -1, when i = 7. I don't know why it's iterating, and I dont know why it starts with returning 1.

  2. Even though in every case i does not equal what System.Array.IndexOf(allKeys, key1) returns, it passes the first if condition and sets all of the values in my array to true.

      public class InputKey : MonoBehaviour {
         
                             public bool[] allKeys;
                             private bool key1;
                             private bool key2;
                             private bool key3;
         
                         void Start () {
                          allKeys = new bool[] { key1, key2, key3 };
                         }
                         
                         public void Key1Pushed () {
                                 for(int i = 0; i < allKeys.Length; i++) {
                                     if (i == System.Array.IndexOf(allKeys, key1)) {
                                         allKeys [i] = true;
                                     } else if (i != System.Array.IndexOf(allKeys, key1)) {
                                         allKeys [i] = false;
                                     }
                                 }
                         }
    
    

Any help would be appreciated,

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

1 Reply

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

Answer by NoseKills · Oct 05, 2016 at 06:48 AM

A bool is a value type, so when you assign a boolean to another boolean variable, you don't create a reference to the original boolean, instead the value of the first gets copied to the second. In other words, you don't get 2 variables referencing the same piece of data, you get 2 separate variables with the same value.

You can easily test this

 key1 = false;
 allKeys = new bool[] { key1 };
 key1 = true;
 
 Debug.Log(key1); // true
 Debug.Log(allKeys[0]); // false
 // Changing key1 doesn't change the copy in the array & vice versa

For the same reason IndexOf() doesn't work as you probably think it does in this case. Because bool is a value type, IndexOf will just find the first index that holds the same value as the second parameter. You can test this too

 key1 = false;
 key2 = false;
 allKeys = new bool[] { key1, key2 };
 
 Debug.Log(System.Array.IndexOf(allKeys, key1))); // 0
 Debug.Log(System.Array.IndexOf(allKeys, key2))); // 0
 // Both return 0 because index 0 contains false and both
 // lines check to find the index of "false" in the array

Since your Key1Pushed() handles only 1 index/key, you don't have to use IndexOf(). I believe you could just do

 for(int i = 0; i < allKeys.Length; i++) {
     allKeys [i] = i == 0; // set index 0 to true, others to false
 }

You could even make the index to be a function parameter

 public void KeyPushed (int keyIndex) {
     for(int i = 0; i < allKeys.Length; i++) {
          allKeys [i] = i == keyIndex;
     }
 }

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 Wylie-Modro · Oct 05, 2016 at 11:24 PM 0
Share

That makes much more sense. Thank you!

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

74 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

Related Questions

Button locking/unlocking for loop using an object array & playerprefs 1 Answer

C# Having troubles with creating/referencing a random list. 1 Answer

How do you play an array of audio clips in sequence with dialogue system on mouse click? 0 Answers

How to load multiple sprites at runtime from List? 1 Answer

For Loop looping out of Loop Condition 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