Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by stavbi1 · Jun 30, 2019 at 06:27 PM · scripting probleminheritancemonobehaviour

How to inherit from a class that inherits from MonoBehaviour

Im trying to implement a base class such as this:

 public class Entity : MonoBehaviour
 {
     public float speed = 0.05f;
     public float maxHP = 100f;
 
     protected float currentHP = 0;
 
     public float GetMaxHP()
     {
         return this.maxHP;
     }
 
     public float GetCurrentHP()
     {
         return this.currentHP;
     }
 
     public float GetSpeed()
     {
         return this.speed;
     }
 
     public void DamageHP(float damage)
     {
         this.currentHP -= damage;
     }
 }
 

and inherit it from this class:

 public class PlayerBehavior : Entity
 {
 
     void Start()
     {
     }
 
     void Update()
     {
     }
 }

and when I try to drag the PlayerBehaviour script to the player, an error pops out:

Can't add script behaviour AssemblyInfo.cs. The script needs to derive from MonoBehaviour!

Comment
Add comment · Show 4
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 xxmariofer · Jun 30, 2019 at 06:44 PM 0
Share

your code is fine, can you try creating 2 new scripts and check if the error persists? in case is some error with class overriding or something like that, you are attaching the playerbehaviour cs to the gameobject rigth?

avatar image stavbi1 · Jun 30, 2019 at 07:22 PM 0
Share

@xxmariofer yes Im attaching it to the gameobject, when I attach the entity class it works but not the player

avatar image xxmariofer stavbi1 · Jun 30, 2019 at 07:49 PM 0
Share

Can ylu try access the methods fron the ñarent class? Gives you errors?

avatar image TrveGanondox · Oct 30, 2020 at 12:07 AM 0
Share

I'm having the same problem. I think the issue is that Unity is failing to recognize that the class is a subclass of $$anonymous$$onoBehavior because for some reason the linkage was never generated. I've also noticed that VisualStudio is being terrible and it's not recognizing any classes in any of the other files, but once saved everything compiled through Unity fine. I don't recall Visual Studio being this bad when I used it in the past for projects unrelated to Unity, so I think the problem might be related to that.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Zimogor · Nov 15, 2020 at 06:44 PM

World "Entity" is reserved by Unity for so called "Entity Component System". So when you inherit from Entity, you inherit not from your Entity, but from ECS Entity. Rename you class name, your resolve name collision somehow.

Comment
Add comment · 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

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

262 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Inheriting from a class that inherits from monobehaviour 3 Answers

Check if script is a particular Class 1 Answer

Start() and Awake() not called on MonoBehaviour 1 Answer

What is the best way to avoid or reduce the amount of monobehaviours in your scripts? 1 Answer

Get reference to dynamically added script 1 Answer


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