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 Mrroundtree22 · Feb 24, 2021 at 04:17 PM · loopbooleanfor-loop

Even more loop problems

 if(!loopBool[i]){
  Script2.ActivateScript2();
  }

I have a bool array called loopBool. I want to be able to loop through the array to check if each element in the array is true or not. If it's true then it calls ActivateScript2() which is located in a separate script.

The problem is that I have this function tied to a button. So I press the button. The function gets called no problem. I press it again. Nothing happens. Because [i] is now true ActivateScript2() does not get called. I tried putting it within a for loop like this:

 for (int i = 0; i < max; i++)
 {
          if (!loopBool[i])
         {
             stats.Activate();
         }        
 }

But that doesn't work either as ActivateScript2 gets called multiple times which is not what I want (see my previous question "Loop problems" for more) I only want it to run ONCE but I don't want it to only run so long as [i] != true. So I don't know what to do. Any ideas? Ask if you want clarification.

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

Answer by pauldarius98 · Feb 24, 2021 at 07:17 PM

Ok, so i readed the other post too and if i got it right, you want Script2.ActivateScript2() to be called when you press the button for the first 3 times, right? IE: I press the button, ActivateScript2 gets called, I press it the second time and it gets called again, i press it 3rd time and it gets called again and when i press it the 4th time, nothing should happen, right?

If that is the case, than instead of a bool array i would use a counter, like in the code below

 int counter = 3; //
 
 public void ActivateScript1()
 {
    if (counter <= 0)
    {
        return;
     }
 
    counter--;
    Script2.ActivateScript2();
 }

If I'm wrong then please provide more informations

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 Mrroundtree22 · Feb 25, 2021 at 01:12 AM 0
Share

Thank you for responding.

 if(!loopBool[i]){
   Script2.ActivateScript2();
   }

This is the code that causes all the problems and it is very difficult for me to articulate why but basically I want the ActivateScript2() function to only activate if I: 1.press the button, 2.the bool in the array is presently not true 3. after those conditions are fulfilled the script activates and bool becomes true and 4. it will then "continue" to the next bool that is not true and do the same thing.

Once a bool is true the Activate() function should not work on that. Activate should just activate whenever the button is pushed and check which bools are true or false. The only way I know how to do this is by using a loop but then each bool gets "Activated" which I don't want. Am I making any sense? Because I'm finding it harder and harder to explain what I'm trying to do honestly.

avatar image
0

Answer by Ermiq · Feb 25, 2021 at 02:40 AM

After reading your comment in the other answer, I think I understand what you want. Probably. On the button click the program should look into the loopBool, find a fist value that is currently false, switch it to true, done, wait a for the next click. Right? If so then:

 for (int i = 0; i < max; i++)
 {
     if (!loopBool[i])
     {
         stats.Activate();
         loobBool[i] = !loopBool[i]; // set the value to opposite (was false, becomes !false, therefore true)
         break; // stop the 'for()' loop, because we already found what we wanted
     }
 }
Comment
Add comment · Show 3 · 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 Mrroundtree22 · Feb 25, 2021 at 09:06 PM 0
Share

I'm afraid not. The problem remains. It's like this:


[False] [False] [False]


I press the button. It checks is loopBool[i] (<- "i" is the problem) false? Yes! Then:


[True] [False] [False]


I press the button again. It checks is loopBool[i] false? No! (Because the first one is true even though I want the second one to turn true so then nothing happens, that's the problem).

avatar image Ermiq Mrroundtree22 · Feb 26, 2021 at 03:40 AM 0
Share

So, what is the max? From the results you describe you have max = 1, and the loop does check only the first element in the loopBool. You need, for example, check all the elements in the loopBool like this:

 for (int i = 0; i < loopBool.Length; i++)
 {
     if (!loopBool[i])
     {
         stats.Activate();
         loobBool[i] = !loopBool[i];
         break;
     }
 }
avatar image Mrroundtree22 Ermiq · Feb 26, 2021 at 02:24 PM 0
Share

Yep did that. Doesn't work. I'm kinda going crazy. I feel like I've tried everything at this point.

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

116 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

Related Questions

Making an animation stop once it finishes (trouble with booleans) 1 Answer

How to deactivate all GameObject in a array, except last one 4 Answers

Rearranging pre-existing objcets into a grid 2 Answers

Getting an error with my for/in loop! 3 Answers

How do couroutines handle If Statements within a for loop? 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