Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Event catch mistake

Discussion in 'Scripting' started by canc953, Apr 27, 2024.

  1. canc953

    canc953

    Joined:
    Oct 15, 2023
    Posts:
    2
    This class invokes the event.

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class SpecialTurn : BattleState
    7. {
    8.  
    9.     public static event StateAction onEnterSpecial;
    10.     public static event StateAction onExitSpecial;
    11.  
    12.     public override void EnterState()
    13.     {
    14.         if (onEnterSpecial != null)
    15.         {
    16.             StartCoroutine(timer());
    17.             onEnterSpecial.Invoke();
    18.             Debug.Log("Timerdan önce");
    19.         }
    20.     }
    21.  
    22.     public override void ExitState()
    23.     {
    24.         onExitSpecial?.Invoke();
    25.     }
    26.  
    27.     public override void updateState()
    28.     {
    29.         throw new System.NotImplementedException();
    30.     }
    31.  
    32.     public IEnumerator timer()
    33.     {
    34.         for (float time = 10; time >= 0; time -= Time.deltaTime)
    35.         {
    36.             //Add UI some timer component
    37.             //Timer.setTime(time);
    38.             Debug.Log("Time : " + time);
    39.             yield return null;
    40.         }
    41.         ExitState();
    42.     }
    43. }

    This class receives the onEnterSpecial event

    Code (CSharp):
    1.  
    2. using System;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. public class PlayAreaHandler : MonoBehaviour
    8. {
    9.     private void OnEnable()
    10.     {
    11.         SpecialTurn.onEnterSpecial += enableEnemyTurnCardArea;
    12.         SpecialTurn.onExitSpecial += disableEnemyTurnCardArea;
    13.     }
    14.  
    15.     private void OnDisable()
    16.     {
    17.         SpecialTurn.onEnterSpecial -= enableEnemyTurnCardArea;
    18.         SpecialTurn.onExitSpecial -= disableEnemyTurnCardArea;
    19.     }
    20.  
    21.     public void enableEnemyTurnCardArea()
    22.     {
    23.         enemyTurnBox.SetActive(true);
    24.     }
    25.  
    26.     public void disableEnemyTurnCardArea()
    27.     {
    28.         Debug.Log("Play area içindeyim");
    29.         enemyTurnBox.SetActive(false);
    30.     }
    31. }
    onSpecialEnter successfully invokes another class but does not invoke this. Do you have any idea why this happens?
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,943
    Perhaps because of EnterState only starting the coroutine when an event is assigned. Whether this is the cause or not, it's simply bad practice. Do not branch code based on whether someone other script is currently subscribed to an event, this causes the event subscription to have side-effects.

    Use it like so:
    Code (CSharp):
    1.     public override void EnterState()
    2.     {
    3.             StartCoroutine(timer());
    4.             onEnterSpecial?.Invoke();
    5.             Debug.Log("Timerdan önce");
    6.     }
    And you should be using non-static events since SpecialTurn doesn't have the same lifetime as the events which may continue to be subscribed to when changing the scene, for example. It will also not support multiple instances of SpecialTurn.
     
    samana1407 likes this.
  3. canc953

    canc953

    Joined:
    Oct 15, 2023
    Posts:
    2
    Firstly, thanks for your reply. We will fix that bad practice part that you mentioned.
    Secondly, another class that also uses invoke of onEnterSpecial works fine but the enableEnemyTurnCardArea method of PlayerAreaHandler which also uses the same event does not work. Do you have any idea why this happens?
     
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,943
    No idea. Did you try debugging? You can quickly find out whether and in what order the event is subscribed to / unsubscribed from, and invoked.
     
    Bunny83 likes this.