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 /
This question was closed Jan 23, 2019 at 10:11 PM by Obsessi0n for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by Obsessi0n · Jan 23, 2019 at 09:17 PM · scripting problemscript.

Using Polymorphism Override

I have a script with two classes a parent and a child. I am trying to override the parent method, but it is not working what am I doing wrong? The script below shows an example of what I am trying to achieve.

  public class Parent : MonoBehaviour
         {
             public virtual void Start()
             {
         
             }
         }
         
         public class Child: Parent
         {
             public override void Start()
             {
               //Do Something
             }
         }
 

Comment
Add comment · Show 1
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 Hellium · Jan 23, 2019 at 09:45 PM 0
Share

I don't have the problem (in Unity 2018.1.9). Are you sure the two classes are in their own file, correctly named?

Parent.cs, attached to a cube

 using UnityEngine;
 
 public class Parent : $$anonymous$$onoBehaviour
 {
     protected virtual void Start()
     {
         Debug.Log( "Parent" );
     }
 }


Child.cs, attached to another cube

 using UnityEngine;    
 
 public class Child : Parent
 {
     protected override void Start()
     {
         Debug.Log( "Child" );
     }
 }

The console outputs the correct messages.

However, be careful, if you don't explictely call base.Start() in Child.cs, the code inside Parent's Start function won't be automatically called by the Child component you have attached to a gameObject.

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by xxmariofer · Jan 23, 2019 at 09:22 PM

thats the correct way of doing it, whats the exact problem you are facing? the parent Start is the one getting called rather than the child? are you aware that the start method wont be called unless you manually call the start method since the parent doesnt derive from monobehaviour

Comment
Add comment · Show 7 · 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 Obsessi0n · Jan 23, 2019 at 09:26 PM 0
Share

$$anonymous$$y bad the actual script is derived from $$anonymous$$onoBehaviour.

Replace the "//Do something" with Deblug.Log("Test"). When I attach the script to an object on unity and then play the debug is never called.

I really dont understand why.

I even tried that exact same script on a new object and it didnt work...

avatar image xxmariofer Obsessi0n · Jan 23, 2019 at 09:38 PM 0
Share

Yes i have test it and works fine :) ill do a quick gif give me asecond but is a bit messy since i was using one of $$anonymous$$e projects

Well, uploading gifs is hard, is taking ages, maybe i can share a drive link with the gif haha

avatar image xxmariofer xxmariofer · Jan 23, 2019 at 09:51 PM 0
Share

here is the gif ill remove it once you checked it, couldnt do it bigger and with better quality for trying to uploaded it here but i think you get the idea.

test is the cs with the child class and the parent class is just in the gamemanager cs for saving some time.

Show more comments

Follow this Question

Answers Answers and Comments

244 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

Related Questions

Assigning a PreFab to a pre existing variable. 0 Answers

Creating Splines from empties in script 0 Answers

Why is the code below not setting the value for the timer? 0 Answers

Multiple GameObjects with the same Script are sharing the same bool values when they shouldn't. 1 Answer

How can I add/remove/destroy more or less objects at runtime ? 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