Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Check if variable is true in all objects within an array?

Discussion in 'Scripting' started by generalmcmutton, Jun 29, 2013.

  1. generalmcmutton

    generalmcmutton

    Joined:
    May 20, 2010
    Posts:
    42
    Evening guys.

    At the moment, I'm attempting to create a lock system for interactive objects, like a door that can't be opened until a lever is pulled. To that end, I've got this bit in my door-opening code:

    Code (csharp):
    1. for (var locks in lockObjects){
    2.         lockScript = locks.GetComponent(switchStuff);
    3.        
    4.         if(lockScript.isActive)
    5.             isLocked = false;
    6. }
    7.  
    8. if(!isLocked  Input.GetButtonDown("Use")  isClosed)
    9.         Open();
    Variable isActive is in a script on the lever and is made true if the lever is pulled.

    It works well enough if I only want one lock, but I need two in this instance. I've got two levers in the lockObjects array that should have to be active for the door to open, but it's opening if only one of them is active.

    Is it possible to make it check the variables in all of the array's objects?
     
  2. SomeDude

    SomeDude

    Joined:
    Feb 16, 2012
    Posts:
    95
    I think you could put isLocked = false above the for loop, and put if(!lockScript.isActive) isLocked = true;


    Code (csharp):
    1. isLocked = false
    2.  
    3. for (var locks in lockObjects){
    4.  
    5.         lockScript = locks.GetComponent(switchStuff);        
    6.  
    7.         if(!lockScript.isActive)
    8.  
    9.             isLocked = true;
    10.             break;
    11. }
    12.  
    13. if(!isLocked  Input.GetButtonDown("Use")  isClosed)
    14.         Open();
     
    Last edited: Jun 29, 2013
  3. Zibber

    Zibber

    Joined:
    Jun 22, 2013
    Posts:
    86
    First, we will assume we need all lockObjects to have isActive set to true in order for isLocked to be false. We will then assume before the for loop that the door is not locked. We will then check all the lockObjects and if we find any that have isActive false, we will immediatly set isLocked to true and break out of the loop. Since as soon as we find out any lever is not pulled, there is no need to check the rest. Hope that was clear enough.

    Code (csharp):
    1. isLocked = false;
    2.  
    3. for (var locks in lockObjects) {
    4.  
    5.     lockScript = locks.GetComponent(switchStuff);
    6.  
    7.     if (!lockScript.isActive) {
    8.         isLocked = true;
    9.         break;
    10.     }
    11. }
    12.  
    13. if (!isLocked  Input.GetButtonDown("Use")  isClosed)
    14.     Open();
    EDIT: looks like I'm too slow haha
     
  4. Eiznek

    Eiznek

    Joined:
    Jun 9, 2011
    Posts:
    374
    Simple solution If I'm understanding what your wanting correctly.

    My understanding is that you want all lockObjects to have its isActive set to true before unlocking.

    Code (csharp):
    1.  
    2. var activeCount = 0;
    3.     for (var locks in lockObjects){
    4.             lockScript = locks.GetComponent(switchStuff);
    5.    
    6.             if(lockScript.isActive){
    7.                 if(activeCount >= lockObjects.Length() ) // Or Count? if your using a list {
    8.                     isLocked = false;
    9.                 }
    10.                 activeCount ++;
    11.             }
    12.     }
    13.  
    14.  
    15.     if(!isLocked  Input.GetButtonDown("Use")  isClosed)
    16.             Open();
    17.  
     
  5. generalmcmutton

    generalmcmutton

    Joined:
    May 20, 2010
    Posts:
    42
    Ah, thanks guys; I tried the first solution(s) and it ended up working perfectly! Much obliged!
     
  6. SomeDude

    SomeDude

    Joined:
    Feb 16, 2012
    Posts:
    95
    Eiznek's solution is probably semantically(?) better. Mine is slightly dirty I guess.

    Though I think his activeCount ++; would have to go above that inner if statement.
     
    Last edited: Jun 29, 2013
  7. generalmcmutton

    generalmcmutton

    Joined:
    May 20, 2010
    Posts:
    42
    I couldn't get that one to work before, but after moving activeCount, as you suggested, it does.

    However, I forgot to mention that I wanted the door to be locked again if the levers were deactivated. I managed it via some messing around with Eiznek's code, and I ended up with this:

    Code (csharp):
    1.     var activeCount = 0;
    2.  
    3.     for(var locks in lockObjects){
    4.         lockScript = locks.GetComponent(switchStuff);
    5.  
    6.         if(lockScript.isActive)
    7.             activeCount ++;
    8.         else
    9.             activeCount --;
    10.        
    11.         if(activeCount >= lockObjects.Length)
    12.             isLocked = false;
    13.         else
    14.             isLocked = true;
    15.     }
    Thanks again, guys!
     
  8. Eiznek

    Eiznek

    Joined:
    Jun 9, 2011
    Posts:
    374
    I accidentally ended up commenting out one of my {..

    I tend to write code with { on there own separate line. But I was trying to keep it the same coding standard as you are use to.

    You can see it in the other post as well
    Code (csharp):
    1.  
    2. if(activeCount >= lockObjects.Length() ) // Or Count? if your using a list {  <---
    3. {
    4.        isLocked = false;
    5. }
    6.