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?