Search Unity

Trouble with OnClick event

Discussion in 'Scripting' started by jkamphenkel, Apr 19, 2021.

  1. jkamphenkel

    jkamphenkel

    Joined:
    Dec 10, 2019
    Posts:
    51
    So I have a code that generates a list of buttons, one for each enemy on the field. Im trying to write an onclick function that searches the gameobjects/fighters on the field, and compares the names against the text of the button you pressed, and if they match, add them to a list. I would then make the attack to damage to all objects on that list.
    everything else but this is working thus far.
    also, as far as the debugs, the function does run, it just doesn't run the 'foreach' loop in the function.

    Code (CSharp):
    1.  
    2. public void SelectTarget(Button attackChoie)
    3.     {
    4.         foreach (GameObject target in lists.combatants)
    5.         {
    6.             if (attackChoice.GetComponentInChildren<Text>(true).text == target.name)
    7.             {
    8.                 attackTargetList.Add(target);
    9.                 Debug.Log("List ran");
    10.                 Debug.Log(target.name);
    11.             }
    12.             else
    13.             {
    14.                 Debug.Log("Else");
    15.             }
    16.         }
    17.         Debug.Log("Target Select Ran");
    18.     }
    19.  
    20. }
    This is the function that instantiates the buttons, if that's at all helpful:

    Code (CSharp):
    1.  
    2.  
    3.   public List <Button> DisplayTargets()
    4.     {
    5.         attackChoiceListOn = true;
    6.         ColorBlock green = attackChoice.colors;
    7.         green.normalColor = Color.green;
    8.         ColorBlock red = attackChoice.colors;
    9.         red.normalColor = Color.red;
    10.         attackOrderPanel.SetActive(true);
    11.         attackAlert.enabled = false;
    12.         for (int t = 0; t < lists.combatants.Count;t++)
    13.         {
    14.        
    15.             Button attackButton  = Instantiate(attackChoice);
    16.        
    17.             attackButton.transform.SetParent(verticalList);
    18.             attackButton.GetComponentInChildren<Text>().text = lists.combatants[t].name;
    19.             if (lists.combatants[t].tag == "Player")
    20.             {
    21.                 attackButton.colors = green;
    22.             }
    23.             else
    24.             {
    25.                 attackButton.colors = red;
    26.             }
    27.             Debug.Log(lists.combatants[t].tag);
    28.             attackChoiceList.Add(attackButton);
    29.         }
    30.         return attackChoiceList;
    31.     }
    32.     }
    I also tried making the SelectTarget function return a list, and then have an void function that runs the SelectTarget function, so I could run it as onClick, but this of course didn't work. I hope this is clear enough, I'm really stuck! if you need any more details or screenshots, Im glad to post.
     
    Last edited: Apr 20, 2021
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    How or where is SelectTarget actually called?

    If it is being called and you see a "Target Select Run" in your console, but none of the other debugs, then lists.combatants must be empty.
     
  3. jkamphenkel

    jkamphenkel

    Joined:
    Dec 10, 2019
    Posts:
    51
    Target select is being ran in the onclick event in the instantiated buttons.