- Home /
Issues Inheriting classes and monoBehaviour
I am trying to create a system of classes for my enemy characters. My parent class is 'Enemy' and the child classes are 'Enemy1' and 'Enemy2'. I can get the objects to works perfectly when I have all the functions in one big script but I want to be able to recycle existing functions from base classes instead of rewrite them every time.
I'm trying to test a simple function which works great when written in the same non custom class but when I try to extend the same function from a parent class my object does nothing and I get this error:
You are trying to create a MonoBehaviour 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
here's my Two classes:
Script Enemy1
#pragma strict
class Enemy1 extends Enemy
{
function Start () {
Speed = 4;
}
function Update () {
var Poop = Enemy();
Poop.MoveInPath();
}
}
Script Enemy
#pragma strict
public class Enemy extends MonoBehaviour
{
var Speed : int;
function MoveInPath()
{
transform.Translate(Speed * Time.deltaTime, 0, 0, Camera.main.transform);
}
}
Can anyone tell me what I'm doing wrong? Should I even be trying to use classes in unity's java script?
Answer by vexe · Jun 13, 2014 at 04:09 PM
If you just google "You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed" you'd get tons of results. The reason you can't do that, is that when a Component
is created (MonoBehaviour is a Component) there is some internal cooking/ behind the scene prepping for the the object in the unmanaged C++ side of Unity, not just a managed representation. For UnityEngine.Components you should always use AddComponent
Unity is a component-based engine. In a component-based design objects don't exist by themselves, but they're the sum of their individual components. It works like this: You have GameObjects
(entities) - these a gameObject is basically a Component holder, it holds components. You give your objects new behaviours by adding to them new components (Rigidbody, BoxCollider, a MonoBehaviour etc)
So for you to instantiate your Enemy
object, since it's a MonoBehaviour, you'd have to add it to a gameObject via myGo.AddComponent< Enemy >();
(C#) and myGo.AddComponent.(); (JS) (use generic whenever you can)
I know, it can be annoying dealing with new comers like me because sometimes we ask horrible questions. I did google it though and it seems that a very large majority of answers were in C#.
I did follow your suggestion and added "var Poop = GetComponent.();" right above "Poop.$$anonymous$$oveInPath();". After adding the Enemy script to an empty gameObject in Unity it's working beautifully with no errors or glitches.
Your patience and help is greatly appreciated! Thanks vexe!
I recommend you use C#. It's much powerful, more flexible and hides less things from you than JS - Syntax makes more sense as well. Like, one could barely even notice the '.' difference between the generic GetComponent
in C# and JS. And btw what is 'poop'? sounds perverted. Just saying.
Your answer
Follow this Question
Related Questions
Using AddComponent to add a Sub Class using a String 4 Answers
Making a static class derive from MonoBehaviour in C# 3 Answers
How to store data in script and attach it later? 0 Answers
Will using a Monobehaviour script versus a non-Monobehaviour script use less memory? 2 Answers
How to serialize fields for both base and derived classes? 0 Answers