- Home /
WaitUntil doesn't work and the coroutine starts anyways
Hello I've been struggling for hours and I can't fix this problem I'm a newbie and I'm trying to make my script drop EXP after death of a monster But problem is I can't do that at all, I tried using Void Update and failed. And then tried Coroutine which showed progress but problem exp is dropped right away from the start as if the "WaitUntil",which should wait until monsters are dead, isn't there.
Also I'm developing this script further on the CreatorKit Beginner(which is making me struggle a lot)
using System;
using CreatorKitCodeInternal;
using UnityEngine;
using System.Collections.Generic;
using System.Collections;
using Random = UnityEngine.Random;
namespace CreatorKitCode
{
public class XPDrop : MonoBehaviour
{
public StatSystem Stats;
// Start is called before the first frame update
void Start()
{
StartCoroutine(DropXP());
}
// Update is called once per frame
void Update()
{
}
IEnumerator DropXP()
{
yield return new WaitUntil(DEAD);
XPManager.instance.AddXP(100);
}
bool DEAD()
{
if(Stats.CurrentHealth == 0)
{
return true;
}
else
{
return false;
}
}
}
}
Please help me cause I'm really sad after spending lots of hours trying to fix it but failing miserably at least if I can't fix this directly, I want to make a new one that drops experience and honestly this is really disappointing.
Are you sure Stats.CurrentHealth is actually 0? Could it be negative? $$anonymous$$ight be safer to do
bool dead() => Stats.CurrentHealth <= 0;
Well this code should work but I don't know about the rest. Put Debug.Log(Stats.CurrentHealth) in update and make sure it's the correct value.
Answer by sacredgeometry · Dec 01, 2020 at 02:38 AM
Have you (immediately) initialised the CurrentHealth field/ property (i.e. to something other than 0 i.e ints default value) ?
If not that will be why. If you have I will change this to a comment.