Search Unity

Question Override Function not being called

Discussion in 'Scripting' started by Kindleia, Jul 2, 2022.

  1. Kindleia

    Kindleia

    Joined:
    Jul 3, 2020
    Posts:
    3
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Test : MonoBehaviour
    6. {
    7.     void Start()
    8.     {
    9.         Debug.Log("Started");
    10.         test1();
    11.     }
    12.  
    13.     public virtual void test1(){}
    14. }
    15.  
    16. public class testing : Test
    17. {
    18.     public override void test1()
    19.     {
    20.         Debug.Log("Override called");
    21.     }
    22. }
     
  2. Kindleia

    Kindleia

    Joined:
    Jul 3, 2020
    Posts:
    3
    "Started" is being printed but "Override called" is not.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Sounds like you're adding a
    Test
    to your GameObject when you intend to and a
    testing


    If you put those two classes in the same file, that's almost certainly what is happening
     
    Bunny83 and Kindleia like this.
  4. Kindleia

    Kindleia

    Joined:
    Jul 3, 2020
    Posts:
    3
    Yup. Separated the two classes and now it works. Thanks
     
    Bunny83 and Kurt-Dekker like this.