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 cjtouhey · Aug 01, 2019 at 06:57 AM · arrayarraysbooleanscycle

Activating Bool Array in sequence.

What i want:

To be able to activate a the next / previous bool in the array in order from 0 to 4 / 4 to 0

     public bool[] selectNext;
 
     void Start()
     {
         selectNext = new bool[4];
         
         
     }
     private void Update()
 
 // here i want my input for scrolling through the bools
     {
         if (Input.GetKeyDown(KeyCode.D)) { "Next bool = true previous bool = false }
     }
 
  if (Input.GetKeyDown(KeyCode.A)) { "Previous = true Next bool = false }
     }


If anyone could help achieve this or explain how to achieve this i would forever be in your debt.

Chris

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 Dragate · Aug 01, 2019 at 08:49 AM

You can use Array.IndexOf(). It will return the index of the first occurence of the element you're looking for. In your circumstances, that first occurence will be the only occurence so this function will serve your needs.

 if (Input.GetKeyDown (KeyCode.D)) { 
     int index = System.Array.IndexOf(selectNext, true);
     if (index < selectNext.Length - 1 && index != -1) {
         selectNext [index] = false; // Make current element false
         selectNext [index + 1] = true; // Make next element true
     }
 }
 
 if (Input.GetKeyDown (KeyCode.A)) { 
     int index = System.Array.IndexOf(selectNext, true);
     if (index > 0) {
         selectNext [index] = false; // Make current element false
         selectNext [index - 1] = true; // Make previous element true
     }
 }
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 cjtouhey · Aug 01, 2019 at 01:35 PM 0
Share

Awesome! Thanks for the new information. This will help me achieve my final idea.

Absolute legend!

avatar image
0

Answer by Bunny83 · Aug 01, 2019 at 01:17 PM

When only one of them should be active at a time, you really shouldn't use an array for this. This just adds redundancy and unnecessary complexity. Just use a single int value and you get the same functionality.

 int selected = 0;
 int count = 4;
 
 void Update()
 {
     if (selected < count-1 && Input.GetKeyDown (KeyCode.D))
     {
         selected++;
     }
     if (selected > 0 && Input.GetKeyDown (KeyCode.A))
     {
         selected--;
     }
 }

To see if selected is a certain value you can directly replace your old conditions like:

 if (selectNext[1])

with

 if (selected == 1)


If instead of clamping the min and max to 0 and count-1 you want it to wrap around, you can simply do:

     if (Input.GetKeyDown (KeyCode.D))
     {
         selected++;
         selected = (count+selected)%count;
     }
     if (Input.GetKeyDown (KeyCode.A))
     {
         selected--;
         selected = (count+selected)%count;
     }

This will make it wrap around automatically. So when you are at selected 0 and press A it will wrap around to "count-1" so 3 in this case. Likewise when at the top end when selected is 3 and you add one you get back down to 0. So the first variant will just stay at the min / max value (which is 0 / 3 for 4 elements) and the second one just wraps around.

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 cjtouhey · Aug 01, 2019 at 01:37 PM 0
Share

Thanks for the idea, unfortunately for my end result i specifically need bools.

I already had the knowledge of doing this, but thanks for you help many anyways.

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

127 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

Related Questions

Vector3 Array Empties Its Self Immediately After Being Initialized 1 Answer

Variable Vs Array? 2 Answers

How do I use array's to spawn gameobjects(C#)? 3 Answers

Finding the amount of true booleans - Arrays 1 Answer

Cycle Through GameObjects in Array Issue. 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