0

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?

.

zakjma
  • 2,030
  • 12
  • 40
  • 81
  • 5
    OnTrackableStateChanged isnt virtual so you [can't override it](http://stackoverflow.com/questions/1853896/is-it-possible-to-override-a-non-virtual-method) – Nitro.de Mar 18 '16 at 08:17

1 Answers1

2

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(...) { }
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • You could add [this question](http://stackoverflow.com/questions/1853896/is-it-possible-to-override-a-non-virtual-method) to your answer so OP dont need to ask if there is a way around ;) – Nitro.de Mar 18 '16 at 08:19
  • 1
    Does OP know what those keywords mean? I think that question is already a step further in the process. – Patrick Hofman Mar 18 '16 at 08:20
  • This example donesn't has a virtual keyword http://www.akadia.com/services/dotnet_polymorphism.html – zakjma Mar 18 '16 at 08:21
  • 1
    Indeed, so drop it from your list as a useful tutorial. – Patrick Hofman Mar 18 '16 at 08:21
  • If you would read a bit further your tutorial says `Only if a method is declared virtual, derived classes can override this method if they are explicitly declared to override the virtual base class method with the override keyword.` – Nitro.de Mar 18 '16 at 08:23
  • True, but the sample should follow that. The program doesn't give the desired behavior now and it introduces compiler warnings. @Nitro.de – Patrick Hofman Mar 18 '16 at 08:24