Search Unity

Question Stopping a function in a script when i recall it.

Discussion in 'Scripting' started by guilbotq, Jan 9, 2024.

  1. guilbotq

    guilbotq

    Joined:
    Dec 11, 2023
    Posts:
    4
    (First of all, sorry if my english is bad)
    So i have this async function for when i pickup an item, it activate a GameObject, then wait for 3 seconds and deactivate it.
    But if i pickup multiple items during those 3 seconds, it stacks the script and force it to deactivate my object.
    Any solution to just stop the function when i reuse it ?

    Code (CSharp):
    1. async void TriggerPickupPopUp(string ItemName, Sprite itemSprite)
    2. {
    3.  
    4.     pickupAlert.SetActive(true);
    5.  
    6.     pickupName.text = ItemName;
    7.  
    8.     pickupImage.sprite = itemSprite;
    9.  
    10.     await Task.Delay(3000);
    11.  
    12.     pickupAlert.SetActive(false);
    13.  
    14.    
    15. }
    Thanks for your help.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,676
  3. TomTheMan59

    TomTheMan59

    Joined:
    Mar 8, 2021
    Posts:
    356
  4. guilbotq

    guilbotq

    Joined:
    Dec 11, 2023
    Posts:
    4
    i tweaked it a bit but it works now, and i find this easier than using async functions, but i guess both works well.
    Will this be much better than using the timer mentionned above ? If yes i might start to look into it.
     
  5. TomTheMan59

    TomTheMan59

    Joined:
    Mar 8, 2021
    Posts:
    356
    It's up to you and your project! glad it worked!