Creating Classes and Passing them to Newly Created Classes

Hi

I am working on a dynamic behaviour tree for AI behaviour but encountered this weird issue that I just can’t wrap my head around.

I have this function which scans for nearby entities. It creates a new “GoalNode” and a new “Subject”, by using their constructors. When I run Scan(), It also invokes SetBehaviour(), but it always says the Subject I pass into and store in it is null.

public class AI : MonoBehaviour
Scan()
{
newNode = new GoalNode(new Subject(newAI));
goals.Add(newNode);
newNode.SetBehaviours();
}

public class GoalNode : MonoBehaviour
{
public Subject subject;

public GoalNode(Subject _subject)
{
subject = _subject;
}
public void SetBehaviours()
{
if (subject != null)
{
*** This is where the problem lies, it’s always null ***
}
}

public class Subject : MonoBehaviour
{
public AI ai;
public Subject(AI _ai)
{
ai = _ai;
}
}

You can’t create instances of MonoBehaviours with the “new” keyword. You have to use AddComponent<>();

Is there a good reason you need Subject and GoalNod to extend MonoBehaviour?