The question is answered, right answer was accepted
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
}
}
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.
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
$$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...
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
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.
Follow this Question
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