M
M
MrAfitol2021-04-23 12:59:03
Unity
MrAfitol, 2021-04-23 12:59:03

What to do if the coroutine does not start?

I wanted to create a loop with a delay, but after looking at a lot of guides, I got this result:

private void OnChangingItem(ChangingItemEventArgs ev)
        {
            if (Check(ev.NewItem))
            {
                if (ev.Player.Health < 50)
                {
                    ev.Player.AddItem(ItemType.SCP500);
                    Timing.RunCoroutine(Healing());
                    //return;
                }
            }
        }

private IEnumerator<float> Healing()
        {
            ev.Player.AddItem(ItemType.SCP018);
            Player.AddItem(ItemType.SCP207);
            while (true)
            {
                ev.Player.AddItem(ItemType.SCP268);
                Player.Health++;
                yield return Timing.WaitForSeconds(3f);
            }
        }

But for some reason, the coroutine does not want to start, maybe I didn’t add something?

I can send you the full code if that's not enough.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
i__egor, 2021-04-23
@i__egor

Coroutines are started as: StartCoroutine(Healing());
What is Timing? write simply: yield return new WaitForSeconds(3f);
With this logic, you will start a new coroutine every time you have OnChangingItem. You can try stopping this before running it again: StopCoroutine(Healing())

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question