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.
Quoteerror CS0021: Cannot apply indexing with [] to an expression of type `bool'
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 [1]) {
Debug.Log ("red button 2 pressed");
}
}
12 minutes ago, davejones said: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"); } }
This is being developed in the unity game engine.