Monobehaviour new keyword


I’m trying to create a layered behavior system, but seem that monobehaviour can’t use the “new keyword”

Here is the code:

    void Start () {
 GameObject leader = Instantiate(CubePrefab) as GameObject;
 Actor leaderActor = leader.GetComponent();
 BehaviorBorderAvoid borders = new BehaviorBorderAvoid(0.5f,10);
 Behavior wanderer =  new BehaviorWander(0.3f,60);
 leaderActor.Speed = 30; 
 leaderActor.Direction = Actor.GetRandomDirection();
 leaderActor.behaviorList.Add(new BehaviorGamepad(0.7f));


behaviorList is always empty! Any solution?

Yes you create new MonoBehaviour derivatives by using AddComponent onto a GameObject.

Then use getters and setters (properties). So add the behavior then use properties (basically public vars) to pass the values you need passing OR when Awake or Start is called when the component is added/created then GET the values from elsewhere as needed.