count occurrences of targets unity

I have this snippet inside the function protected virtual void OnTrackingFound() from DefaultTrackableEventHandler.cs, the script that is automatically added on every image target.
What I’m trying to do, is if some targets are found, then counter++

This is the snippet:

protected virtual void OnTrackingFound() {
    ...

    if (mTrackableBehaviour.TrackableName.ToString() == "IMAGE1") {
        print("FOUND LIFE 1");
        countLivesFromBoard++;
    }
    if (mTrackableBehaviour.TrackableName.ToString() == "IMAGE2") {
        print("FOUND LIFE 2");
        countLivesFromBoard++;
    }
    if (mTrackableBehaviour.TrackableName.ToString() == "IMAGE3") {
        print("FOUND LIFE 3");
        countLivesFromBoard++;
    }
}

where countLivesFromBoard is a global variable, outside the function.

So if one of IMAGE1, IMAGE2, IMAGE3 are found, I want the counter to increase. For example if only IMAGE1 and IMAGE2are found, then the counter will be 2. But this is not working properly, because no matter how many image targets I scan, the counter is every time 1. It seems that every time it enters on function OnTrackingFound, it resets.

How can I solve this issue? Thanks!

With the few information you have provided, it’s hard to help you.

From the look of it, I would suggest to declare countLivesFromBoard as static so that your instances share the same value. Don’t forget to decrement the value in the OnTrackingLost method.

Other suggestion, maybe cleaner, and more convenient if you want to display the number of images on screen, would be to delegate the responsibility of counting the images to another entity so you get rid of the static variable.

// New class to create and to attach to another object, then drag & drop in the inspector
public ImageCounter ImageCounter;

 protected virtual void OnTrackingFound() {
     ImageCounter.AddTrackable(mTrackableBehaviour);
 }

 protected virtual void OnTrackingLost() {
     ImageCounter.RemoveTrackable(mTrackableBehaviour);
 }

public class ImageCounter : MonoBehaviour
{
     private int countLivesFromBoard;
     public Text CountLivesFromBoardText;
     public void AddTrackable(TrackableBehaviour trackableBehaviour)
     {
         if (trackableBehaviour.TrackableName.ToString() == "IMAGE1") {
             print("FOUND LIFE 1");
             countLivesFromBoard++;
         }
         if (trackableBehaviour.TrackableName.ToString() == "IMAGE2") {
             print("FOUND LIFE 2");
             countLivesFromBoard++;
         }
         if (trackableBehaviour.TrackableName.ToString() == "IMAGE3") {
             print("FOUND LIFE 3");
             countLivesFromBoard++;
         }
         CountLivesFromBoardText = countLivesFromBoard.ToString();
     }
     public void RemoveTrackable(TrackableBehaviour trackableBehaviour)
     {
         if (trackableBehaviour.TrackableName.ToString() == "IMAGE1") {
             print("LOST LIFE 1");
             countLivesFromBoard--;
         }
         if (trackableBehaviour.TrackableName.ToString() == "IMAGE2") {
             print("LOST LIFE 2");
             countLivesFromBoard--;
         }
         if (trackableBehaviour.TrackableName.ToString() == "IMAGE3") {
             print("LOST LIFE 3");
             countLivesFromBoard--;
         }
         CountLivesFromBoardText = countLivesFromBoard.ToString();
     }
}