Reputation:
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.
Upvotes: 0
Views: 4763
Reputation: 18035
If I understand you correctly, you need your AI script to get all the Boid instances in the scene and do something with them.
If this is the case, then there are multiple ways of doing this, a simple way would be:
public Boids[] boids
field declared in your AI scriptHere is some example scripts:
public class AI : MonoBehaviour
{
public Boid[] boids;
void Update()
{
foreach (Boid boid in this.boids)
{
// Do something with boid
}
}
}
public class Boid : MonoBehaviour
{
public float searchRadius;
// Anything else
}
Upvotes: 0
Reputation: 6864
Pretty easy to google...
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>()
Upvotes: 1