Use array index value in if statement

I have created a GameObject array that contains 2 GameObjects with the same tag. I am trying to get each index value and use the index value for an OnClick event on each of the 2 GameObjects with the tag. The 2 GameObjects that have the same tag have a button component.

I am trying to access the array index for each of the 2 GameObjects and use them for an OnClick method that recognises the appropriate index through conditional statements. The method “public void red_buttons_mthd ()” is attached to each OnClick for the button component of the 2 GameObjects .

  • GameObject array using FindGameObjectsWithTag.

  • Access each array index

  • Separate indexes using if statements

  • Access function from OnClick of each button component

I get the error seen in the quote below. I have searched the internet and seen numerous forums on the issue of accessing index values and using them in conditional statements, however I can’t seem to find a conclusive answer. If you need any more information do please let me know.

public void red_buttons_mthd ()
    {
        GameObject [] red_buttons_go = GameObject.FindGameObjectsWithTag ("red buttons");
  
  

  

        for (int i = 0; i < red_buttons_go.Length; i++)



            if (red_buttons_go[0] == true ){




      

            Debug.Log ("red button 1 pressedd");
        }
          

            else if (red_buttons_go[1] == true ) {
              
      



                Debug.Log ("red button 2 pressed");

            }
                  
  
    }
else if (red_buttons_go[1] == true [1])

true is a boolean, not an array, it should just be else if (red_buttons_go[1] == true)

also why parse your array and use hard index (not using i)?

I am not sure why to be honest.

When I change the syntax and press the red_button_go [1] the Debug.Log only shows "Debug.Log (“red button 1 pressedd”);

Are you wanting something along these lines? Although I suspect something is missing from line 5 (hence my big comment there) :-

GameObject[] red_buttons_go = GameObject.FindGameObjectsWithTag("red buttons");

for (int i = 0; i < red_buttons_go.Length; i++)
{
    if (red_buttons_go[i] <presumably you want to call a method on the button that returns a bool here?> )
    {
        Debug.Log("red button " + i + " pressed");
    }
}

This is what I believe I need yes. So when the method is called a bool is returned and each array index of the GameObject has its own bool thats returned.

I am looking into using a list of GameObjects with the same tag and accessing the index of each GameObject in the list.

Ok. And will that be a list of buttons?

Yes, that will be a list of buttons that are components of the GameObjects with the same tag.

There must be a way to linearly search through an array of GameObjects and use each index from the array in a an if statement. This is what I am looking into.

The answer to that is, of course, yes there is. :slight_smile:

An “if” statement just takes a bool if( some_condition ). So the question is, what is the condition you are wanting? Can you state it in natural language and we can see if we can find the appropriate call to make.

The condition I am wanting is to know which button has been pressed pressed from the GameObject array. I have a GameObject array which has been created using GameObject.FindGameObjectsWithTag . Each GameObject from the array has a button component.

What type of buttons are they? Is it a Toggle Group or is it a series of UI Buttons that have an OnClick event?

It is a series of UI Buttons that have an on OnClick event.

Ok, so next question- what is it you are trying to do by working out what was pressed that cannot be done by an OnClick handler? :slight_smile:

Because I have more than one Gameobject with the same tag and each onclick handler is referencing the same function. The function than determines which index from the array of GameObjects has been pressed.

You can call a method with a parameter fron OnClick. Then supply a unique value in the parameter from each button.

I suppose I could than use that value in the if statement to determine which array elements was pressed.

So in simple terms I have 2 scripts. One script script (a) contains delegates and events. The seconds script (script b) will contain an event handler method that subscribes to a function in scirpt a. In script “a” there will be a function in which a boolean is set to true and that function will be added to the OnClick handler of the button. When the button is pressed there will be a subscriber to the event in script “b”.

  • script “a” (main logic controller) contains delegates and events

  • script “a” has function that sets boolean to true OnClick of button

  • Script “B” is subscribed to function in script “a” so is waiting for the button to be pressed.

I want to drive Boolean logic off the individual clicks. I am looking at completely changing the way I approach the project. I am looking at adding methods to the OnClick event handler of each button component. The method will contain boolean logic used to determine which event handler methods should be called. I don’t think I will create buttons as you have but rather make use of delegates and events to execute the desired functions.