Search Unity

UnityEvren issue

Discussion in 'Editor & General Support' started by DerDicke, Nov 2, 2019.

  1. DerDicke

    DerDicke

    Joined:
    Jun 30, 2015
    Posts:
    292
    I didn't find anything on the net, so maybe I just overlook something.
    I wrote an OnEnabledDo script that executes an UnityEvent when triggered.
    This works nice and dandy... until I execute the same function on two similar scripts on one GameObj, e.g. calling AddItemToInventory.Add() in two components. Like this:

    Code (CSharp):
    1. namespace Freeman
    2. {
    3.     public class AddItemToInventory : MonoBehaviour
    4.     {
    5.         public ItemData itemData;
    6.         public float waitTimeBeforeAdd;
    7.  
    8.         DialogHandler dialogHandler; // just to execute the coroutine here, because this node might be disabled before wait time ends.
    9.  
    10.         public void AddItem()
    11.         {
    12.             if (!enabled) return;
    13.             if (dialogHandler) dialogHandler.StartCoroutine(CoAdd());
    14.             else StartCoroutine(CoAdd());
    15.         }
    16.  
    17.  
    18.         void Awake()
    19.         {
    20.             dialogHandler = GetComponentInParent<DialogHandler>();
    21.         }
    22.  
    23.         void Start()
    24.         {
    25.             // allows to disable component
    26.         }
    27.  
    28.  
    29.         IEnumerator CoAdd()
    30.         {
    31.             //
    32.             yield return new WaitForSeconds(waitTimeBeforeAdd);
    33.             Inventory.me.CreateAndAddItem(itemData);
    34.             InfoText.me.ShowText("You received " + itemData.itemName);
    35.         }
    36.  
    37.     }
    38. }
    used like this:
    upload_2019-11-2_17-16-57.png

    If I do this, only the first AddItem() is executed.
     

    Attached Files:

    Last edited: Nov 2, 2019