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 Dangerface · Nov 17, 2020 at 08:36 PM · listloops

Check bool state for each Gameobject in a list

Scenario: I have a door that is triggered open by a pressure plate. In some cases I want the door to trigger open only by multiple pressure plates at the same time.

What I want is to check the list of pressure plates to se if the are all triggered so I can open the door.

What I have now finds the script of all pressure plates in the scene and not the list, and it only checks the last script in my array is true, to open door.

Hope someone can help me in the right direction :) Heres my code:

     private presurePlateScript presurePlateScript;
     public List<GameObject> padList = new List<GameObject> ();
     //private List<GameObject> padListScript = new List<GameObject> ();
     public bool openDoor;
     
     public presurePlateScript[] _padListScript;
     
     // Use this for initialization
     void Start () {
         openClose = false;
         
         foreach(GameObject go in padList) //for every object
         {
             _padListScript = FindObjectsOfType(typeof(presurePlateScript)) as presurePlateScript[];         
         }
     }
     
     // Update is called once per frame
     void Update () {
         foreach(presurePlateScript sc in _padListScript)
      {
         if(sc.isPlateTrigger == true)//if the boolean is true then win
         {
           openDoor = true;
         }
         else
         {
           openDoor = false;
           
         }
      }
 
         if(openDoor)
         {
             
             StartCoroutine(doorOpen(gameObject.transform, doorPosB, 0.3f));
         }
 if(!openDoor)
         {
             StartCoroutine(doorClose(gameObject.transform, doorPosA, 0.3f));
         }    


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 Hellium · Nov 17, 2020 at 10:00 PM

 // Fill list in inspector, get rid of the public list of gameObjects and the Start method
 public presurePlateScript[] _padListScript;
 
 private bool doorIsOpen;
 
 // Update is called once per frame
 void Update ()
 {
     bool allPlatesPressed = true;
     foreach(presurePlateScript sc in _padListScript)
     {
         if(sc.isPlateTrigger == false)
         {
             allPlatesPressed = false;
             break;
         }
     }
 
 if(!doorIsOpen && allPlatesPressed)
 {
     StopAllCoroutines();
     StartCoroutine(doorOpen(gameObject.transform, doorPosB, 0.3f));
 }
 else if(doorIsOpen && !allPlatesPressed)
 {
     StopAllCoroutines();
     StartCoroutine(doorClose(gameObject.transform, doorPosA, 0.3f));
 }  
Comment
Add comment · Show 5 · 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 Dangerface · Nov 18, 2020 at 10:50 AM 0
Share

Hi @Hellium Thanks, I did not know you could just assign scripts to a list like that in the inspector, very helpful!

Unfortunately the above script does not really work. I simplified my own script as it is closer to working than your provided solution, but I still have the problem that the: foreach(presurePlateScript sc in _padListScript) { if(sc.isPlateTrigger == true) { openDoor = true; } else { openDoor = false; } Only checks the last script in the list of sc.isPlateTrigger is true. I want it to check all sc.isPlateTrigger is true, and only then set openDoor = true;

Here is my complete updated script:

 private bool openClose;
     public bool allPlatesPressed;
     public presurePlateScript[] _padListScript;
     
     // Use this for initialization
     void Start () {
         openClose = false;
         allPlatesPressed = false;
     }
     
     // Update is called once per frame
     void Update () {
         foreach(presurePlateScript sc in _padListScript)
          {
             if(sc.isPlateTrigger == true)
             {
             allPlatesPressed = true;
             }
             else
             {
             allPlatesPressed = false;
             }
         }
 
         if(allPlatesPressed && !openClose)
         {
             
             StartCoroutine(doorOpen(gameObject.transform, doorPosB, 0.3f));
         }
         if(!allPlatesPressed && openClose)
         {
             StartCoroutine(doorClose(gameObject.transform, doorPosA, 0.3f));
         }            
     }
     IEnumerator doorOpen(Transform transform, Vector3 position, float timeTo$$anonymous$$ove)
     {
         exitDoorCollider.enabled = false;
              var t = 0f;
             while(t < 1)
             {
                     t += Time.deltaTime / timeTo$$anonymous$$ove;
                     transform.position = Vector3.Lerp(doorPosA, doorPosB, t);
                     yield return null;
             }
             openClose = true;
              
     }
     IEnumerator doorClose(Transform transform, Vector3 position, float timeTo$$anonymous$$ove)
     {
             exitDoorCollider.enabled = true;
              var t = 0f;
             while(t < 1)
             {
                     t += Time.deltaTime / timeTo$$anonymous$$ove;
                     transform.position = Vector3.Lerp(doorPosB, doorPosA, t);
                     yield return null;
             }
             openClose = false;
              
     }

avatar image Hellium Dangerface · Nov 18, 2020 at 11:03 AM 0
Share

The key is precisely to not have an if-else but to do like I did:

  allPlatesPressed = true;
  foreach(presurePlateScript sc in _padListScript)
  {
      if(sc.isPlateTrigger == false)
      {
          allPlatesPressed = false;
          break;
      }
  }
avatar image Dangerface Hellium · Nov 18, 2020 at 11:10 AM 0
Share

Thank you so much for the help. It works now as intended. Now I just need to find out why lol ^^the above does not really make sense to me, yet :)

Show more comments

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

143 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 avatar image avatar image avatar image avatar image 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

A node in a childnode? 1 Answer

Add existing variables to an array? 1 Answer

How to instantiate a prefab one by one in foreach loop 3 Answers

Argument out of range 0 Answers

Difference between List<> and Array[] 2 Answers


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