Search Unity

  1. How can our website serve you better? Give us your feedback. Take our survey and let us know.
    Dismiss Notice

Checking for range of arrays are empty

Discussion in 'Scripting' started by davidnibi, Mar 22, 2021.

  1. davidnibi

    davidnibi

    Joined:
    Dec 19, 2012
    Posts:
    342
    I have this:

    Code (CSharp):
    1.     private bool CheckHandleCompleteEmpty()
    2.     {
    3.         for (int i = 0; i < pickupHandleComplete.Length; i++)
    4.         {
    5.             if (!pickupHandleComplete[i])
    6.             {
    7.                 return true;
    8.             }
    9.         }
    10.         return false;
    11.     }
    It should check each pickupHandleComplete is empty as:

    Code (CSharp):
    1. free1 = CheckHandleCompleteEmpty();
    and shorthand:

    Code (CSharp):
    1. free1 = (!(pickupHandleComplete[0]) && !(pickupHandleComplete[1]));
    and return free1 as true if they are - it isn't working for me although I've used this a few times, and is always returning nothing.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    6,012
    What do you mean by returning nothing?

    If I understand correctly (and I'm not sure that I do), your method is supposed to tell you if all elements of the array are false? If that's what you want, your logic is not right. You're bailing out and returning true as long as you find any false element in the array.

    What you want to do is bail out if you find any true element in the array:

    Code (CSharp):
    1.     private bool CheckHandleCompleteEmpty()
    2.     {
    3.         for (int i = 0; i < pickupHandleComplete.Length; i++)
    4.         {
    5.             if (pickupHandleComplete[i])
    6.             {
    7.                 return false;
    8.             }
    9.         }
    10.         return true;
    11.     }
     
  3. davidnibi

    davidnibi

    Joined:
    Dec 19, 2012
    Posts:
    342
    Sorry, I mean pickupHandleComplete is never true in this case - I currently have two elements in the array, which can have any amount of elements - I'm trying to detect if any element pickupHandleComplete is true - but it's never returning false or true in that routine.
     
  4. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,199
    That's not possible -- if the method is getting called, it's either returning false or true.
     
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    6,012
    Then it would be like this:
    Code (CSharp):
    1.     private bool CheckHandleCompleteEmpty()
    2.     {
    3.         for (int i = 0; i < pickupHandleComplete.Length; i++)
    4.         {
    5.             if (pickupHandleComplete[i])
    6.             {
    7.                 return true;
    8.             }
    9.         }
    10.         return false;
    11.     }
    But what on earth do you mean by "it's returning nothing"?
     
    Joe-Censored likes this.
unityunity