0
\$\begingroup\$

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");
}
}
\$\endgroup\$
3
  • \$\begingroup\$ Have you tried using StartCoroutine(WaitForTIme()); instead of using strings? \$\endgroup\$ Commented May 28, 2020 at 14:24
  • \$\begingroup\$ I have, it yields the exact same result \$\endgroup\$ Commented May 28, 2020 at 14:26
  • \$\begingroup\$ One thing to watch for: do you try to create an instance of your concrete derived class anywhere with new ConcreteClass() instead of someGameObject.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\$ Commented May 28, 2020 at 16:53

1 Answer 1

1
\$\begingroup\$

I'm unable to reproduce this problem. Testing with the following AbstractClass:

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() {
        Debug.Log("Starting coroutine");
        IsInCooldown = true;
        StartCoroutine(WaitForTime());
        // StartCoroutine("WaitForTime"); // Also works, but it's less efficient.
    }

    protected IEnumerator WaitForTime() {
        Debug.Log("The wait is starting...");
        yield return new WaitForSeconds(CooldownSeconds);
        IsInCooldown = false;
        Debug.Log("The wait is over!");
    }
}

And this ConcreteClass:

public class ConcreteClass : AbstractClass
{
    override protected string iD { get { return name; } }
    override protected int CooldownSeconds { get { return 1; } }

    protected override void TurnOn() {
        Debug.Log("Turning on");
    }

    private void Start() {
        Activate();
    }
}

I get no exceptions, and the console log prints:

  • Turning on
  • Starting coroutine
  • The wait is starting...
  • The wait is over!

So apparently the answer to "How to call StartCourotine in abstract class?" is "exactly the way you're already doing it".

Your problem may be coming from somewhere else in your project. Please try to reproduce this problem in a new, empty project containing as few scripts as possible, then edit your question to detail every step we need to follow to cause the problem. This will help narrow down what unseen code/scene configuration is responsible, so we can find solutions for your situation.

\$\endgroup\$
2
  • \$\begingroup\$ I think it comes from the fact that I cannot add this script to any object because it is an abstract class. So maybe misbehavior doesn't work. \$\endgroup\$ Commented May 29, 2020 at 16:25
  • \$\begingroup\$ Presumably what you're adding to your object is the concrete class that derives from this, no? \$\endgroup\$ Commented May 29, 2020 at 16:26

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.