Log in or register to post comments

The object of type 'ImageTargetBehaviour' has been destroyed but you are still trying to access it

November 25, 2013 - 6:32pm #8

Hi there !

I am trying to follow CloudRecoTarget sample with mutiple targets but i got stuck at one point with an error of

"The object of type 'ImageTargetBehaviour' has been destroyed but you are still trying to access it ".  

It seem that object of type of "ImageTargetBehaviour" will always destroy when " OnNewSearchResult " is fire .

In the sample CloudRecoTarget project they have code as in following  but why thier CloudRecoTarget (object of type ImageTargetBehaviour)is not destroy? and while CloudRecoBehaviour is set to false

mCloudRecoBehaviour.CloudRecoEnabled = false;

why they can track another targets?

Hopping for your kindly help. Thanks

 

    public void OnNewSearchResult(TargetFinder.TargetSearchResult targetSearchResult)
    {
        // This code demonstrates how to reuse an ImageTargetBehaviour for new search results and modifying it according to the metadata
        // Depending on your application, it can make more sense to duplicate the ImageTargetBehaviour using Instantiate(), 
        // or to create a new ImageTargetBehaviour for each new result

        // Vuforia will return a new object with the right script automatically if you use
        // TargetFinder.EnableTracking(TargetSearchResult result, string gameObjectName)
        
        //Check if the metadata isn't null
        if(targetSearchResult.MetaData == null)
        {
            return;
        }

        // enable the new result with the same ImageTargetBehaviour:
        ImageTargetBehaviour imageTargetBehaviour = mImageTracker.TargetFinder.EnableTracking(targetSearchResult, mParentOfImageTargetTemplate);

        if (imageTargetBehaviour != null)
        {
            // stop the target finder
            mCloudRecoBehaviour.CloudRecoEnabled = false;
            
            // Calls the TargetCreated Method of the SceneManager object to start loading
            // the BookData from the JSON
            mContentManager.TargetCreated(targetSearchResult.MetaData);
        }
    }

 

The object of type 'ImageTargetBehaviour' has been destroyed but

November 25, 2013 - 6:39pm #7

The trackable returned with the Cloud Recognition search result is overwritten by subsequent search results. So only one Cloud Recognition trackable can exist at a time. 

You can track Cloud Recognition targets and Device Database targets simultaneously though.

The object of type 'ImageTargetBehaviour' has been destroyed but

November 25, 2013 - 6:46pm #6

But how the CloudRecoTarget sample does?
I do not see where they clone the ImageTagetTemplate.
Can you explain more clearly please?

The object of type 'ImageTargetBehaviour' has been destroyed but

November 26, 2013 - 8:40am #5

Here is a simpler example of how to handle search results using a template - https://developer.vuforia.com/forum/cloud-recognition/cloud-recognition-tutorial-scripts

Here is the relevant code..

 

public void OnNewSearchResult(TargetFinder.TargetSearchResult targetSearchResult)
    {
        // duplicate the referenced image target
        GameObject newImageTarget = Instantiate(ImageTargetTemplate.gameObject) as GameObject;
         
        GameObject augmentation = null;
         
        string model_name = targetSearchResult.MetaData;
         
         
        if( augmentation != null )
            augmentation.transform.parent = newImageTarget.transform;
 
        // enable the new result with the same ImageTargetBehaviour:
        ImageTargetBehaviour imageTargetBehaviour = mImageTracker.TargetFinder.EnableTracking(targetSearchResult, newImageTarget);
         
        Debug.Log("Metadata value is " + model_name );
         
         
         
        switch( model_name ){
             
            case "teapot" :
                 
                Destroy( imageTargetBehaviour.gameObject.transform.Find("Dragon").gameObject );
                
                break;
             
            case "dragon" :
                 
                Destroy( imageTargetBehaviour.gameObject.transform.Find("teapot").gameObject );
             
                break;
             
        }
 
        if (imageTargetBehaviour != null)
        {
            // stop the target finder
            mCloudRecoBehaviour.CloudRecoEnabled = false;
        }
    }

 

and this tutorial will show you how to set-up a Cloud Reco app that uses this event handler - https://developer.vuforia.com/resources/tutorials/creating-cloud-recognition-apps-unity

 

 

The object of type 'ImageTargetBehaviour' has been destroyed but

January 21, 2014 - 2:09am #4

Thank you for your help DavidBeard

I found the solution to my problems. I did not disable CloudRecoBehaviour while i did not Instantiate(ImageTargetTemplate.gameObject).

Here is what i have come up with.

    public void OnNewSearchResult(TargetFinder.TargetSearchResult targetSearchResult)
    {
 
        // enable the new result with the same ImageTargetBehaviour:
        ImageTargetBehaviour imageTargetBehaviour = mImageTracker.TargetFinder.EnableTracking(targetSearchResult, mParentOfImageTargetTemplate);        
 
        if (imageTargetBehaviour != null)
        {
            // stop the target finder
            mCloudRecoBehaviour.CloudRecoEnabled = false;
            mContentManager.TargetCreated(targetSearchResult.MetaData);
        }
 
    }

And everytime when we want to enable CloudRecoBehaviour we have to check.

if (mCloudRecoBehaviour.CloudRecoInitialized && !mCloudRecoBehaviour.CloudRecoEnabled)
{

  mCloudRecoBehaviour.CloudRecoEnabled = true;

}

The object of type 'ImageTargetBehaviour' has been destroyed but you are still trying to access it

March 24, 2018 - 2:56pm #3

Still have a problem on Cloud Book Sample !!

TargetSearchResult  xxxx could not be enabled for tracking.

Unity3d

Version 2017.3.1f1 (fc1d3344e6ea)

Mon, 29 Jan 2018 15:28:29 GMT

Branch: 2017.3/release



Vuforia 7.0.47

 

AttachmentSize
Image icon 20180325053330.png206.56 KB
Image icon 20180325053436.png46.69 KB
Image icon 20180325053629.png9.41 KB

The object of type 'ImageTargetBehaviour' has been destroyed but you are still trying to access it

March 26, 2018 - 11:01am #2

1966392197 wrote:

Still have a problem on Cloud Book Sample !!

TargetSearchResult  xxxx could not be enabled for tracking.

Unity3d

Version 2017.3.1f1 (fc1d3344e6ea)

Mon, 29 Jan 2018 15:28:29 GMT

Branch: 2017.3/release



Vuforia 7.0.47

 

I am not able to reproduce this. Do you have a license key added to this project? If so, what kind of key?

Thanks,

Vuforia Support

The object of type 'ImageTargetBehaviour' has been destroyed but you are still trying to access it

February 15, 2021 - 4:15am #1

Hey, I am facing the same issue. I've tried to follow the code provided by DavidBeard but it seems it's obsolete since the interface ICloudRecoEventHandler doesn't exist anymore. I am trying to play different videos from different ImageTargets that are allocated on the CloudDataBase. I did most part of it and it's working as expected. But When I try to restart the scanner to scan other image it doesn't work.

 

Quote:

using UnityEngine;

using Vuforia;

using UnityEngine.Video;

using UnityEngine.UI;

public class SimpleCloudRecoEventHandler : MonoBehaviour

{

    public ImageTargetBehaviour ImageTargetTemplate;

    public GameObject video;

    public Button m_PlayButton;

    public RectTransform m_ProgressBar;

    private CloudRecoBehaviour mCloudRecoBehaviour;

    private VideoPlayer videoPlayer;

    private bool mIsScanning = false;

    private string mTargetMetadata = "";

    void Start()

    {

        video = GameObject.Find("Video");

        videoPlayer = video.GetComponent<VideoPlayer>();

        VuforiaARController vuforia = VuforiaARController.Instance;

        vuforia.RegisterVuforiaStartedCallback(OnVuforiaStarted);

        vuforia.RegisterOnPauseCallback(OnPaused);

    }

    private void OnVuforiaStarted()

    {

        CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);

    }

    private void OnPaused(bool paused)

    {

        if (!paused)

        {

            CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);

        }

    }

    // Register cloud reco callbacks

    void Awake()

    {

        mCloudRecoBehaviour = GetComponent<CloudRecoBehaviour>();

        mCloudRecoBehaviour.RegisterOnInitializedEventHandler(OnInitialized);

        mCloudRecoBehaviour.RegisterOnInitErrorEventHandler(OnInitError);

        mCloudRecoBehaviour.RegisterOnUpdateErrorEventHandler(OnUpdateError);

        mCloudRecoBehaviour.RegisterOnStateChangedEventHandler(OnStateChanged);

        mCloudRecoBehaviour.RegisterOnNewSearchResultEventHandler(OnNewSearchResult);

    }

    //Unregister cloud reco callbacks when the handler is destroyed

    void OnDestroy()

    {

        mCloudRecoBehaviour.UnregisterOnInitializedEventHandler(OnInitialized);

        mCloudRecoBehaviour.UnregisterOnInitErrorEventHandler(OnInitError);

        mCloudRecoBehaviour.UnregisterOnUpdateErrorEventHandler(OnUpdateError);

        mCloudRecoBehaviour.UnregisterOnStateChangedEventHandler(OnStateChanged);

        mCloudRecoBehaviour.UnregisterOnNewSearchResultEventHandler(OnNewSearchResult);

    }

    public void OnInitialized(TargetFinder targetFinder)

    {

        Debug.Log("Cloud Reco initialized");

    }

    public void OnInitError(TargetFinder.InitState initError)

    {

        Debug.Log("Cloud Reco init error " + initError.ToString());

    }

    public void OnUpdateError(TargetFinder.UpdateState updateError)

    {

        Debug.Log("Cloud Reco update error " + updateError.ToString());

    }

    public void OnStateChanged(bool scanning)

    {

        mIsScanning = scanning;

        if (scanning)

        {

            // clear all known trackables

            var tracker = TrackerManager.Instance.GetTracker<ObjectTracker>();

            tracker.GetTargetFinder<ImageTargetFinder>().ClearTrackables(true);

        }

    }

    // Here we handle a cloud target recognition event

    public void OnNewSearchResult(TargetFinder.TargetSearchResult targetSearchResult)

    {

        // duplicate the referenced image target

        GameObject newImageTarget = Instantiate(ImageTargetTemplate.gameObject) as GameObject;

        // enable the new result with the same ImageTargetBehaviour:

        ImageTargetBehaviour imageTargetBehaviour = mImageTracker.TargetFinder.EnableTracking(targetSearchResult, newImageTarget);

        TargetFinder.CloudRecoSearchResult cloudRecoSearchResult = (TargetFinder.CloudRecoSearchResult)targetSearchResult;

        // do something with the target metadata

        mTargetMetadata = cloudRecoSearchResult.MetaData;

        // Build augmentation based on target

        if (ImageTargetTemplate)

        {

            ObjectTracker tracker = TrackerManager.Instance.GetTracker<ObjectTracker>();

            tracker.GetTargetFinder<ImageTargetFinder>().EnableTracking(targetSearchResult, ImageTargetTemplate.gameObject);

            Vector2 target = ImageTargetTemplate.GetSize();

            Vector3 temp = video.transform.localScale;

            temp.x = target.x / 9.7f;

            temp.z = target.y / 9.7f;

            video.transform.localScale = temp;



            // stop the target finder (i.e. stop scanning the cloud)

            if (imageTargetBehaviour != null)

            {

                // stop the target finder

                mCloudRecoBehaviour.CloudRecoEnabled = false;

            }

           



        }

        if (mTargetMetadata != "")

        {

            videoPlayer.url = mTargetMetadata;

        }

        Play();

    }

    void Play()

    {

        PauseAudio(false);

        videoPlayer.Play();

        ShowPlayButton(false);

    }

    void PauseAudio(bool pause)

    {

        for (ushort trackNumber = 0; trackNumber < videoPlayer.audioTrackCount; ++trackNumber)

        {

            if (pause)

                videoPlayer.GetTargetAudioSource(trackNumber).Pause();

            else

                videoPlayer.GetTargetAudioSource(trackNumber).UnPause();

        }

    }

    void ShowPlayButton(bool enable)

    {

        m_PlayButton.enabled = enable;

    }

    void OnGUI()

    {

        // Display current 'scanning' status

        GUI.Box(new Rect(100, 100, 200, 50), mIsScanning ? "Scanning" : "Not scanning");

        // Display metadata of latest detected cloud-target

        GUI.Box(new Rect(100, 200, 200, 50), "Metadata: " + mTargetMetadata);

        // If not scanning, show button

        // so that user can restart cloud scanning

        if (!mIsScanning)

        {

            if (GUI.Button(new Rect(100, 300, 200, 50), "Restart Tracking"))

            {

                // Restart TargetFinder

                if (mCloudRecoBehaviour.CloudRecoInitialized && !mCloudRecoBehaviour.CloudRecoEnabled)

                {

                    mTargetMetadata = null;

                    mCloudRecoBehaviour.CloudRecoEnabled = true;

                  

                }

            }

        }

    }

}

Log in or register to post comments