Bellow is my code. I have an abstract class and a inheriting class. This keeps giving me an NullReferenceException, on the StartCoroutine(WaitForTime) when Activate() is called on a InheritedClass instance. I can't figure it out.
public abstract class AbstractClass : MonoBehaviour
{
abstract protected string iD { get; }
abstract protected int CooldownSeconds { get; }
protected bool IsInCooldown { get; set; }
public void Activate()
{
if (!IsInCooldown)
{
TurnOn();
StartCooldown();
}
}
protected abstract void TurnOn();
public void StartCooldown()
{
IsInCooldown = true;
StartCoroutine("WaitForTime");
}
protected IEnumerator WaitForTime()
{
yield return new WaitForSeconds(CooldownSeconds);
IsInCooldown = false;
}
}
public class InheretingClass : AbstractClass
{
protected override int CooldownSeconds { get => 30;}
protected override string collD => "Cool";
protected override void TurnOn()
{
Debug.Log( iD + " Activated");
}
}
StartCoroutine(WaitForTIme());instead of using strings? \$\endgroup\$new ConcreteClass()instead ofsomeGameObject.AddComponent<ConcreteClass>()? This is a common mistake that can cause problems when trying to start a coroutine (among other cases), since there's no game object to run the coroutine on. \$\endgroup\$