Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by SVER · Jun 13, 2014 at 03:48 PM · inheritancemonobehaviourclassesfunctions

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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)

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SVER · Jun 13, 2014 at 07:18 PM 0
Share

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!

avatar image vexe · Jun 13, 2014 at 09:02 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges