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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Is there a way to check a book array and return witch of the bools inside the array returns true?

Discussion in 'Scripting' started by Sylvir, Jun 27, 2015.

  1. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    Expected behaviour:

    I am expecting to have my bool array set up and then run a function that will check through the array and return what of the bools returns true. The way my code is set up only one of these bools in the array is able to become true at once. I am not completely sure how this would work but this is what i have been toying with so far..


    Code (CSharp):
    1. public bool[] fishingPoleBoolArray = new bool[14];
    2.  
    3. void Start()
    4. {
    5. fishingPoleBoolArray[0] = GameControl.control.startingPole;
    6. }
    7.  
    8. //then going through and assigning each of the array values to another bool that is located in my GameControl.control script.
    9.  
    10.  
    11.  
    12.  
    then I have this part that I am not quiet sure how to actually make it go through and check the bool array and return what one is = to true.

    Code (CSharp):
    1. private void CheckWhatBoolReturnsTrue()
    2.     {
    3.    
    4.         //go through the 14 fishing pole bools and return what of the 14 = true
    5.     }
    6.  
    could someone please point me in the direction online that could help me learn how to make a function to scan through a bool array and return witch of the bools is true? Thank you very much. I have been looking online and just trying to use my logic and the unity documentation to work out the way to write this function for a few days and I am not sure how to get it working.
     
  2. Duugu

    Duugu

    Joined:
    May 23, 2015
    Posts:
    241
    A for loop would do the trick. Like:
    Code (CSharp):
    1. for (int x = 0; x < fishingPoleBoolArray.Lenght; x++ )
    2.     {
    3.      if(fishingPoleBoolArray[x] == true){
    4.         return x;
    5.      }
    6. }
     
    Sylvir likes this.
  3. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    Thank you, I have not had much experience with loops yet. That sounds perfect. I will do some reading up on them and then add that into my code. Appreciate it!
     
  4. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    would i need to add the namespace UnityEngine.UI sence you are using a .Length there?

    NVM the length of the array duh. sry lol
     
  5. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396

    this is what i attempted to add in there.
    Code (CSharp):
    1.     private void CheckWhatBoolReturnsTrue()
    2.     {
    3.         for (int x = 0; x < fishingPoleBoolArray.Length; x++ )
    4.         {
    5.             if(fishingPoleBoolArray[x] == true){
    6.                 return x;
    7.             }
    8.        
    9.          Debug.Log("Bool that returns true is : " + x);
    10.         }
    this is the error i am getting: TapToFish.CheckWhatBoolReturnsTrue()': A return keyword must not be followed by any expression when method returns void

    Should I just remove the void from my function as it is now returning something?
     
  6. Maxsiom

    Maxsiom

    Joined:
    Jun 27, 2015
    Posts:
    1
    This method returns an integer. So you need to replace void with int. You also need to return an int if the for loop doesn't find any bool == true in the array (even if you say you won't have that situation). With that function it should work:

    Code (csharp):
    1. private int CheckWhatBoolReturnsTrue() {
    2.     for(int x = 0; x < fishingPoleBoolArray.Length; x++) {
    3.         if(fishingPoleBoolArray[x] == true)
    4.                return x;
    5.         }
    6.     }
    7.     return -1;
    8. }
    Now the function returns the index of the bool == true in your array or it returns -1, if the array doesn't have any bool == true.
     
    Sylvir likes this.
  7. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    cool thank you, that extra part is probably good coding practice just to make sure something doesnt break from some strange error
     
  8. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    hmm for some reason when i added that its not liking the return its saying this
    error CS1519: Unexpected symbol `return' in class, struct, or interface member declaration
    when i just copy and pasted that into it.