Hello Unity Forums,
I have a coroutine which unlocks an item(see below). However when 2 items get unlocked at the same time only one item unlock screen gets shown due to my unlocking logic setting a panel to be active and changing the neccesary information. I tried to make 1 of these coroutines wait by using yield return new WaitWhile(()=> isUnlocking) and then immediatly setting isUnlocking = true. However this does not work due to the bool not changing in time. Does anyone have any advice?
public IEnumerator UnlockItem(Item item)
{
if (!isUnlocked(item))
{
yield return new WaitWhile(()=> isUnlocking);
isUnlocking = true;
needsUpdate = true;
unlockedItemList.Add(item);
itemUnlock.SetActive(true);
itemUnlock.transform.SetAsLastSibling();
Transform itemFrameTransform = itemUnlock.transform.Find("Item Frame");
itemFrameTransform.Find("Item").gameObject.GetComponent<Image>().sprite = item.sprite;
itemFrameTransform.Find("Name").gameObject.GetComponent<TextMeshProUGUI>().text = item.name;
itemFrameTransform.Find("Lore").gameObject.GetComponent<TextMeshProUGUI>().text = item.lore;
AudioManager.instance.Play("ItemUnlock");
}
}
StartCoroutine(GameManager.instance.UnlockItem(fireItem));
StartCoroutine(GameManager.instance.UnlockItem(waterItem));
I use my CallAfterDelay class for delayed action.
CallAfterDelay.cs
using UnityEngine;
using System.Collections;
public class CallAfterDelay : MonoBehaviour
{
float delay;
System.Action action;
// Will never call this frame, always the next frame at the earliest
public static CallAfterDelay Create( float delay, System.Action action)
This file has been truncated. show original
See usage notes at bottom below gist code.
You could modify it to be a CallWhenTrue and give it a test predicate to decide when to proceed.
EDIT: I found I already had a CallWhenTrue so I put it in a gist:
I have been trying to impliment the CallWhenTrue however I got stuck either with it not firing or getting stuck on my WaitWhile. Do you have any suggestions as to how I would implement this?
Nicknotname2:
Hello Unity Forums,
I have a coroutine which unlocks an item(see below). However when 2 items get unlocked at the same time only one item unlock screen gets shown due to my unlocking logic setting a panel to be active and changing the neccesary information. I tried to make 1 of these coroutines wait by using yield return new WaitWhile(()=> isUnlocking) and then immediatly setting isUnlocking = true. However this does not work due to the bool not changing in time. Does anyone have any advice?
public IEnumerator UnlockItem(Item item)
{
if (!isUnlocked(item))
{
yield return new WaitWhile(()=> isUnlocking);
isUnlocking = true;
needsUpdate = true;
unlockedItemList.Add(item);
itemUnlock.SetActive(true);
itemUnlock.transform.SetAsLastSibling();
Transform itemFrameTransform = itemUnlock.transform.Find("Item Frame");
itemFrameTransform.Find("Item").gameObject.GetComponent<Image>().sprite = item.sprite;
itemFrameTransform.Find("Name").gameObject.GetComponent<TextMeshProUGUI>().text = item.name;
itemFrameTransform.Find("Lore").gameObject.GetComponent<TextMeshProUGUI>().text = item.lore;
AudioManager.instance.Play("ItemUnlock");
}
}
StartCoroutine(GameManager.instance.UnlockItem(fireItem));
StartCoroutine(GameManager.instance.UnlockItem(waterItem));
Maybe you can try to make a field that will store the return value of the UnlockItem method and check if the field is null before calling the UnlockItem method?