Declaring a new instance of a class in C#

Unity is not liking this and upon research and a warning from unity

("You are trying to create a MonoBehavior using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively your script can inherit from ScriptableObject or no base class at all.)

I know of AddComponent, but I don't quite think that's what I want.

public Boids boids = new Boids();

Boids is my 2nd class. I saw this work on a tutorial, so I'm kind of confused on why this doesn't work in Monodevelop.


Solution 1:

Pretty easy to google...

http://forum.unity3d.com/threads/119537-You-are-trying-to-create-a-MonoBehaviour-using-the-new-keyword

It's to do with what Boids inherits. New is apparently not the correct way to instantiate the object, it should be instatiated using the unity framework

gameObject.AddComponent<Boids>()