Topic locked

Fail to create Model Target with adding DefaultObserverEventHandler

Unity Editor
August 8, 2022 - 9:03am #1

Unity Version: 2021.3

Vuforia Version: 10.7.2

 

I import the model target into unity which created by MTG. And then add ARCamera game object to the scene. Attch a script to ARCamra.But run with error.

I find that error raised when I add a DefaultObserverEventHandler  and register callback

 

There is the script:

using System.Collections;

using System.Collections.Generic;

using System.IO;

using UnityEngine;

using Vuforia;

public class Test : MonoBehaviour

{

    // Start is called before the first frame update

    void Start()

    {

        VuforiaApplication.Instance.OnVuforiaStarted += OnVuforiaStarted;

    }

    private void OnVuforiaStarted()

    {

        var path = Path.Combine(Application.streamingAssetsPath, "Vuforia/Chair.xml");

        var mt = VuforiaBehaviour.Instance.ObserverFactory.CreateModelTarget(

            path,

            "Chair");

        DefaultObserverEventHandler m = mt.GetComponent<DefaultObserverEventHandler>();

        m.OnTargetFound.AddListener(onTargetFound);

        m.OnTargetLost.AddListener(onTragetLost);

        Debug.Log(mt);

    }

    public void onTargetFound()

    {

        Debug.Log("find");

    }

    private void onTragetLost()

    {

        Debug.Log("not find");

    }

    // Update is called once per frame

    void Update()

    {

    }

}

 

There is the log:

Exception in callback: Object reference not set to an instance of an object

UnityEngine.Debug:LogErrorFormat (string,object[])

Vuforia.Utility.ExtensionMethods.DelegateHelper:InvokeDelegate (System.Delegate,object[])

Vuforia.Utility.ExtensionMethods.DelegateHelper:InvokeWithExceptionHandling (System.Action)

Vuforia.VuforiaApplication:VuforiaStarted ()

System.Delegate:DynamicInvoke (object[])

Vuforia.Utility.ExtensionMethods.DelegateHelper:InvokeDelegate (System.Delegate,object[])

Vuforia.Utility.ExtensionMethods.DelegateHelper:InvokeWithExceptionHandling (System.Action)

Vuforia.Internal.Core.Engine:OnStart ()

Vuforia.Internal.Core.Engine:Start (System.Action)

Vuforia.Internal.Core.Engine:Start ()

Vuforia.VuforiaBehaviour:VuforiaInitialized (Vuforia.VuforiaInitError)

System.Delegate:DynamicInvoke (object[])

Vuforia.Utility.ExtensionMethods.DelegateHelper:InvokeDelegate (System.Delegate,object[])

Vuforia.Utility.ExtensionMethods.DelegateHelper:InvokeWithExceptionHandling<Vuforia.VuforiaInitError> (System.Action`1<Vuforia.VuforiaInitError>,Vuforia.VuforiaInitError)

Vuforia.Internal.Core.Engine:InitOnCameraReady ()

Vuforia.WebCam:HandleFirstWebCamFrame ()

Vuforia.WebCam:<Init>b__33_0 (bool)

Vuforia.Internal.Utility.VuforiaCoroutineUtility/<RunCoroutineWithTimeout>d__1:MoveNext ()

UnityEngine.SetupCoroutine:InvokeMoveNext (System.Collections.IEnumerator,intptr) (at /Users/bokken/buildslave/unity/build/Runtime/Export/Scripting/Coroutines.cs:17)

AttachmentSize
Package icon Chair.zip66.11 KB
Platform: 

Fail to create Model Target with adding DefaultObserverEventHandler

September 5, 2022 - 10:46pm #2

Hey there,

 

The issue seems to be related to GetComponent. You should use AddComponent instead.

 

Kind regards,

Patrick Scheper

Vuforia Engine Product Manager

Topic locked