Advertisement

Use array index value in if statement

Started by July 31, 2018 09:23 AM
10 comments, last by Alberth 6 years, 1 month ago

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. 

  

Quote


error 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. 

You have:

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

This should be:

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

Advertisement
4 minutes ago, Steven Ford said:

You have:

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

This should be:

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

Thanks for the heads up. It has now been corrected. 

I am looking into using a list of GameObjects with the same tag and accessing the index of each GameObject in the list. Effectively create code that controls the click order of the GameObjects in the array.  

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. 

Can't remember exactly, but is the order of items returned by the query guaranteed to be consistent? I think relying on a particular index value to match on could be unreliable and suggests you might be trying to work against the framework. You might be better off naming your red buttons (as well as tagging them as redbuttons) then your code would just be :

foreach(GameObject go in red_buttons_go)

{

if(go)

{

Debug.Log(go.Name+" Pressed.");

}

}

?

Still not quite clear what you're trying to achieve though ? The OnClick event should have information about it's origin could just test that?

Advertisement
8 minutes ago, xexuxjy said:

Can't remember exactly, but is the order of items returned by the query guaranteed to be consistent? I think relying on a particular index value to match on could be unreliable and suggests you might be trying to work against the framework. You might be better off naming your red buttons (as well as tagging them as redbuttons) then your code would just be :

foreach(GameObject go in red_buttons_go)

{

if(go)

{

Debug.Log(go.Name+" Pressed.");

}

}

?

Still not quite clear what you're trying to achieve though ? The OnClick event should have information about it's origin could just test that?

I have created an array of GameObjects. Each GameObject in the array has a UI Button component. Each GameObject item from the array has the same tag. I am trying to find the most effective way to organise and deal with OnClick events for an array of GameObjects with the same tag. 

Sure, but what logic do you want to drive off the individual clicks?

You could have something like the one below (snippets of code from a project) where the buttons are created and their on click listener is passed the button itself. Then in a controller method you can drive some logic from the button you provide. Not sure you need to create separate arrays of objects to handle this? Apologies if I've got completely wrong end of the stick.
 


        menuPanel.ArenaButton = UIFactory.CreateStandardButton("ArenaButton", "Arena", region, buttonPanel).GetComponent<Button>();
        menuPanel.ArenaButton.onClick.AddListener(() => { menuPanel.TownMenuPanel_Click(menuPanel.ArenaButton); });

        menuPanel.ShopButton = UIFactory.CreateStandardButton("ShopButton", "Shop", region, buttonPanel).GetComponent<Button>();
        menuPanel.ShopButton.onClick.AddListener(() => { menuPanel.TownMenuPanel_Click(menuPanel.ShopButton); });

        menuPanel.SchoolButton = UIFactory.CreateStandardButton("SchoolButton", "School", region, buttonPanel).GetComponent<Button>();
        menuPanel.SchoolButton.onClick.AddListener(() => { menuPanel.TownMenuPanel_Click(menuPanel.SchoolButton); });

        menuPanel.LeaveTownButton = UIFactory.CreateStandardButton("LeaveButton", "Leave Town", region, buttonPanel).GetComponent<Button>();
        menuPanel.LeaveTownButton.onClick.AddListener(() => { menuPanel.TownMenuPanel_Click(menuPanel.LeaveTownButton); });




    public void TownMenuPanel_Click(Button control)
    {
        if (control == ShopButton)
        {
            m_townGuiController.NextPanel(m_townGuiController.ShopPanel);
        }
        else if (control == SchoolButton)
        {
            m_townGuiController.NextPanel(m_townGuiController.SchoolPanel);
        }
        else if (control == ArenaButton)
        {
            m_townGuiController.NextPanel(m_townGuiController.ArenaMenuPanel);
        }
        else if (control == LeaveTownButton)
        {
            GameStateManager.SetNewState(GameMode.Overland);        
        }
    }

 

4 minutes ago, xexuxjy said:

Sure, but what logic do you want to drive off the individual clicks?

You could have something like the one below (snippets of code from a project) where the buttons are created and their on click listener is passed the button itself. Then in a controller method you can drive some logic from the button you provide. Not sure you need to create separate arrays of objects to handle this? Apologies if I've got completely wrong end of the stick.
 



        menuPanel.ArenaButton = UIFactory.CreateStandardButton("ArenaButton", "Arena", region, buttonPanel).GetComponent<Button>();
        menuPanel.ArenaButton.onClick.AddListener(() => { menuPanel.TownMenuPanel_Click(menuPanel.ArenaButton); });

        menuPanel.ShopButton = UIFactory.CreateStandardButton("ShopButton", "Shop", region, buttonPanel).GetComponent<Button>();
        menuPanel.ShopButton.onClick.AddListener(() => { menuPanel.TownMenuPanel_Click(menuPanel.ShopButton); });

        menuPanel.SchoolButton = UIFactory.CreateStandardButton("SchoolButton", "School", region, buttonPanel).GetComponent<Button>();
        menuPanel.SchoolButton.onClick.AddListener(() => { menuPanel.TownMenuPanel_Click(menuPanel.SchoolButton); });

        menuPanel.LeaveTownButton = UIFactory.CreateStandardButton("LeaveButton", "Leave Town", region, buttonPanel).GetComponent<Button>();
        menuPanel.LeaveTownButton.onClick.AddListener(() => { menuPanel.TownMenuPanel_Click(menuPanel.LeaveTownButton); });




    public void TownMenuPanel_Click(Button control)
    {
        if (control == ShopButton)
        {
            m_townGuiController.NextPanel(m_townGuiController.ShopPanel);
        }
        else if (control == SchoolButton)
        {
            m_townGuiController.NextPanel(m_townGuiController.SchoolPanel);
        }
        else if (control == ArenaButton)
        {
            m_townGuiController.NextPanel(m_townGuiController.ArenaMenuPanel);
        }
        else if (control == LeaveTownButton)
        {
            GameStateManager.SetNewState(GameMode.Overland);        
        }
    }

 

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. 

 

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. 

My only concern is I don't know if this new approach is flexible/ robust in that changes can be accommodated for easily. I am trying to develop code that is robust. 

This topic is closed to new replies.

Advertisement