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 Samir 1 · Feb 17, 2013 at 04:27 PM · multipleboolforeachgetcomponents

foreach script check if boolean is true

Hello, obviously I have a problem as I am asking a question. So here is my problem,

I have multiple game objects, named block. They all have the same script attached to them. In this script a boolean is set to true when these said blocks connect to each other. This boolean works as i have tested it out thoroughly.

I have another, controller object say, it's purpose is to end the game when all these blocks are connected ie, when all the booleans are true.

I have been trying to figure it out now for hours and i can't seem to find anything.

So here is some of my code:

This is attached to the controller.

obj is the gameobjects that the script with the boolean are attached to.

     foreach(GameObject go in obj) //for every object
     {
        pcs = go.GetComponents(); //pcs of type script
     }
         
     foreach(PipeConnectorScript sc in pcs)//for each script
     {
         if(sc.connected == true)//if the boolean is true then win
         {
             won = true;
         }
         else
         {
             won = false;
         }
     }

  if(won == true)
  {
   Debug.Log("Winning!");
  }

Now what happens is only if some are connected then it still equals true and the log is passed. What I require is that aaallllll the booleans from each object are set to true before winning the game.

AFTER FURTHER INSPECTION

the line

pcs = go.GetComponents();

only returns one component for some reason as i have checked in the inspector. so this could be the problem. Although i do not see why as pcs is an array declared at the top like so.

PipeConnectorScript[] pcs;

I hope i have asked my question in as much detail as possible. If not then i will elaborate further, thanks much appreciated.

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

3 Replies

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

Answer by Samir 1 · Feb 17, 2013 at 05:48 PM

It's alll good guys i figured it out.

I was right in thinking that this line was wrong

pcs = go.GetComponents();

so i replaced it with this line and moved it into the start function.

pcs = FindObjectsOfType(typeof(PipeConnectorScript)) as PipeConnectorScript[];

as all i wanted to really do is find this script.

et voila.

Comment
Add comment · Show 2 · 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 dubbreak · Feb 17, 2013 at 06:07 PM 0
Share

Glad to see you solve your problem. However what Robertbut noticed was definitely a bug as well (you would have run into after solving the only having one component issue).

If left the way it was (without a break) you would get true if the last block check was connected regardless of whether they were all connected.

avatar image Cnote · Nov 30, 2014 at 01:26 AM 0
Share

This answer saved my life!!!

avatar image
1

Answer by robertbu · Feb 17, 2013 at 04:38 PM

The first time you run across something that is not connected, you should break out of your loop:

 foreach(PipeConnectorScript sc in pcs)//for each script
     {
        if(sc.connected == true)//if the boolean is true then win
        {
          won = true;
        }
        else
        {
          won = false;
          break;
        }
     }

There are other ways you can structure this check, but you want to stop processing the first time you find a false. The way it is now, the your last check (T or F) is the one that sticks.

Comment
Add comment · Show 2 · 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 Samir 1 · Feb 17, 2013 at 04:47 PM 0
Share

even though this may help with the code. i have tried it out and i still get the same result, the game is won even though not all are connected.

avatar image dubbreak · Feb 17, 2013 at 05:45 PM 0
Share

Set a break point right after the foreach exits and check the values in the list. Robertbu hit it on the head.

Another way to do what you are doing is to AND the connected together (technically still O(n), but not quite as efficient as breaking on the first false).

 won = true; //set to true before checking
 
 foreach(PipeConnectorScript sc in pcs)//for each script
     {
        won = won && sc.connected;
     }

This is essentially the same as doing: sc1.connected & sc2.connect & sc3.connected... etc

Result should be the exact same as Robertbu's code though (only will end up being true after the loop if all the items are true).

Are you sure you're not accidentally changing the value elsewhere?

avatar image
0

Answer by dubbreak · Feb 17, 2013 at 05:57 PM

Your problem is in your first loop. Each time you loops you are reassigning pcs with the one component from that object.

It should be something along the lines of:

 foreach(GameObject go in obj) //for every object
     {
        pcs.Add( = GetComponent<PipeConnectorScript>()); //pcs of type script
     }

You could also do this via linq:

 Using System.Linq;
 
 var pcs = objs.Where(n => n.GetComponent<PipeConnectorScript>!=null).Select(n => n.GetComponent<PipeConnectorScript>!=null).ToList();


Alternatively you can do it all in one fell swoop (since it looks like this is all in one method).

 won = true;
 foreach(var obj in objs)//for each script
     {
        var pipeScript = obj.GetComponent<PipeConnectorScript>
 
      if (pipeScript !=null)
      {
         won = won && pipeScript .connected;
      }
     }

 
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

10 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

Related Questions

Accessing an Array in another function 1 Answer

Retrieving a variable from ALL scripts on a Game Object 1 Answer

Accessing Booleans from other scripts 0 Answers

How to get variables from a list of a list of gameobjects? 1 Answer

For each (foreach) statement running hundreds of times. 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