title: Why does my Button not get the right Onclick event in Unity?
body: <p>I have implemented the state pattern for my game in Unity.
  Thus I have a main class (BattleSystem) which starts by calling different states.
  One of those states is the AttackState. The AttackState has an attackButton which is an UI Button (using UnityEngine.UI).</p>
  <p>My State class looks like this:</p>
  <pre><code>public abstract class State : MonoBehaviour
  {
      protected BattleSystem battleSystem;
  
      public abstract void Tick();
  
      public virtual void OnStateEnter() { }
      public virtual void OnStateExit() { }
  
      public State(BattleSystem battleSystem) {
          this.battleSystem = battleSystem;
      }    
  }
  </code></pre>
  <p>My StateMachine class looks like this:</p>
  <pre><code>public abstract class StateMachine : MonoBehaviour
  {
      protected State state;
  
      public void SetState(State state) {
          this.state = state;
          state.OnStateEnter();
      }
  }
  </code></pre>
  <p>The Button is in the BattleSystem class like this:</p>
  <pre><code>public class BattleSystem : StateMachine
  {
      public Button attackButton;
   // Start is called before the first frame update
  void Start()
  {
      SetState(new AttackState(this));
  }
     
  </code></pre>
  <p>I dragged the AttackButton object onto my attackButton field in the inspector in the Unity editor.</p>
  <p>My AttackState.cs:</p>
  <pre><code>using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  using UnityEngine.UI;
  
  public class AttackState : State
  {
  
      public AttackState(BattleSystem battleSystem) : base(battleSystem)
      {
  
      }
  
      public override void Tick()
      {
          
      }
  
      public override void OnStateEnter()
      {
         
  
          Debug.Log(&quot;AttackState&quot;);
          battleSystem.attackButton.onClick.AddListener(delegate () { this.Attack(); });
          Debug.Log(battleSystem.attackButton.gameObject.name);
  
      }
  
      public void Attack() {
          Debug.Log(&quot;Attacking&quot;);
          foreach (Transform child in battleSystem.cardDropZone.transform)
          {
              Card card = (Card)child.GetComponent(&quot;Card&quot;);
  
              // inflict damage to opponent equal to attack of cards   
              if (battleSystem.canDamageBeInflicted(card))
              {
                  battleSystem.inflictDamage(card);
                  // adjust crystalCount
                  TurnUtilities.crystalCount -= card.crystalCost;
  
                  // Move card to graveyard after it has been used
                  battleSystem.sendToGraveyard(child);
  
                  battleSystem.attackButton.GetComponentInChildren&lt;Text&gt;().text = &quot;End Turn&quot;;
  
              }
              else
              {
                  Debug.Log(&quot;not enough crystals&quot;);
              }
          }
  
          if (PlayerIsDead())
          {
              battleSystem.isPlayerDead = true;
              battleSystem.SetState(new GameEndingState(battleSystem));
          }
          if (EnemyIsDead())
          {
              battleSystem.isEnemyDead = true;
              battleSystem.SetState(new GameEndingState(battleSystem));
          }
      }
  
  
      bool PlayerIsDead()
      {
  
          if (battleSystem.playerHealthbar.value &lt;= 0)
          {
              return true;
          }
  
          return false;
  
      }
  
  
      bool EnemyIsDead()
      {
          if (battleSystem.enemyHealthbar.value &lt;= 0)
          {
              return true;
          }
  
          return false;
      }
  }
  </code></pre>
  <p>The funny thing is that the <code>Debug.Log(battleSystem.attackButton.gameObject.name);</code> works and gives me the name of the GameObject. The event is not being registered though. What did I miss?</p>
  <p>Picture of my Scene:</p>
  <p><a href="https://i.stack.imgur.com/8dn9r.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8dn9r.png" alt="Scene" /></a></p>
  <p>EDIT: I found out that this code works:</p>
  <pre><code>btn.onClick.AddListener(() =&gt; Debug.Log(&quot;Test&quot;));
  </code></pre>
  <p>Putting the method inside does not work? What is happening here :D</p>