Search code examples
c#unity-game-enginepolymorphismoverridingvuforia

C# function override


I use vuforia sdk to track 2d marker. Now I can handle marker detection and lost. Vuforia sdk has a .cs file. The content of it :

namespace Vuforia
{
    public class DefaultTrackableEventHandler : MonoBehaviour,
                                                ITrackableEventHandler
    {
        private TrackableBehaviour mTrackableBehaviour;
        void Start()
        { ... }

        public void OnTrackableStateChanged(
                                        TrackableBehaviour.Status previousStatus,
                                        TrackableBehaviour.Status newStatus)
        {
            ....
        }
    }
}

I try to override OnTrackableStateChanged function. So I create a new .cs file.

public class ImageTargetScript : DefaultTrackableEventHandler {
     public void OnTrackableStateChanged(
                                            TrackableBehaviour.Status previousStatus,
                                            TrackableBehaviour.Status newStatus)
    {
         Debug.Log("**OVERRIDE FUNCTION**");
    }
}

I drag my new .cs file to game object. When I run this code on unity, I don't see output text as "OVERRIDE FUNCTION".

What is my fault? How can I override this function?

.


Solution

  • Your OnTrackableStateChanged in the base class should be marked virtual, so it can be overridden (now it can't, and you should see a warning about this in your IDE). The override method should have the override keyword in it:

    public virtual void OnTrackableStateChanged(...) { }
    
    public override void OnTrackableStateChanged(...) { }