Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Vuforia OnTrackingFound () with Object reference not set to an instance of an Object C#

Discussion in 'Scripting' started by wahahahaharu, Apr 28, 2017.

  1. wahahahaharu

    wahahahaharu

    Joined:
    Mar 13, 2017
    Posts:
    2
    Hey guys, I'm trying to write a script for creating multiple objects of Vuforia image target at real time, and manage movements of those cloned objects.
    But I got these error

    Exception in callback: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object
    at (wrapper stelemref) object:stelemref (object,intptr,object)
    at test.OnTrackingFound () [0x0006f] in C:\test.cs:74
    at test.OnTrackableStateChanged (Status previousStatus, Status newStatus) [0x0001f] in C:\test.cs:57
    at Vuforia.TrackableBehaviour.OnTrackerUpdate (Status newStatus) [0x00000] in <filename unknown>:0
    at Vuforia.DataSetTrackableBehaviour.OnTrackerUpdate (Status newStatus) [0x00000] in <filename unknown>:0
    at Vuforia.VuforiaExtendedTrackingManager.ApplyTrackingState (Vuforia.TrackableBehaviour trackableBehaviour, Status vuforiaStatus,


    I tried to make 1 Model Transform array and spawn multiple objects with for loop but it gave the same problem.

    Is there something wrong related to Vuforia or?
    Big thanks for everyone.


    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using Vuforia;
    5.  
    6. public class test : MonoBehaviour, ITrackableEventHandler
    7. {
    8.  
    9.     private TrackableBehaviour mTrackableBehaviour;
    10.  
    11.     public Transform myModelPrefab;
    12.     private Transform myModelTrf;
    13.     private GameObject[] myModelTrf1;
    14.  
    15.     public GameObject[] pointers;
    16.     public bool created = false;
    17.     public float speed;
    18.     private int count = 0;
    19.  
    20.     // Use this for initialization
    21.     void Start()
    22.     {
    23.         mTrackableBehaviour = GetComponent<TrackableBehaviour>();
    24.  
    25.         if (mTrackableBehaviour)
    26.         {
    27.             mTrackableBehaviour.RegisterTrackableEventHandler(this);
    28.         }
    29.     }
    30.  
    31.     // Update is called once per frame
    32.     void Update()
    33.     {
    34.         float step = speed * Time.deltaTime;
    35.         float rotateTime = step * 2;
    36.         if (created == true)
    37.         {
    38.  
    39.             for (int i = 0; i < pointers.Length; i++)
    40.             {
    41.                 myModelTrf1[i].transform.position = Vector3.MoveTowards(myModelTrf1[i].transform.position, pointers[i].transform.localPosition, step);
    42.                 myModelTrf1[i].transform.rotation = Quaternion.Slerp(myModelTrf1[i].transform.rotation, pointers[i].transform.localRotation, rotateTime);
    43.             }
    44.  
    45.             created = false;
    46.         }
    47.     }
    48.  
    49.     public void OnTrackableStateChanged(
    50.               TrackableBehaviour.Status previousStatus,
    51.               TrackableBehaviour.Status newStatus)
    52.     {
    53.         if (newStatus == TrackableBehaviour.Status.DETECTED ||
    54.             newStatus == TrackableBehaviour.Status.TRACKED)
    55.         {
    56.             if (count < 40)
    57.             {
    58.                 OnTrackingFound();
    59.                 count++;
    60.             }
    61.  
    62.         }
    63.     }
    64.     private void OnTrackingFound()
    65.     {
    66.         if (myModelPrefab != null)
    67.         {
    68.             Transform myModelTrf = GameObject.Instantiate(myModelPrefab) as Transform;
    69.  
    70.             myModelTrf.parent = mTrackableBehaviour.transform;
    71.             myModelTrf.localPosition = new Vector3(0f, 0f, 0f);
    72.             myModelTrf.localRotation = Quaternion.identity;
    73.             myModelTrf.localScale = new Vector3(0.0005f, 0.0005f, 0.0005f);
    74.  
    75.             myModelTrf1[count] = myModelTrf.gameObject;
    76.             myModelTrf1[count].SetActive(true);
    77.         }
    78.     }
    79. }
    80.  
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    This line is odd, and I don't think it'll work:
    Code (csharp):
    1.             Transform myModelTrf = GameObject.Instantiate(myModelPrefab) as Transform;
    You're instantiating an object of type GameObject, and then typecasting it to type Transform. A GameObject has a transform, but it doesn't convert to one, so I would expect myModelTrf to be null after this line. Try this instead:
    Code (csharp):
    1.             Transform myModelTrf = GameObject.Instantiate(myModelPrefab).transform;
     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    Though looking at the line where the error occurs, I may have been wrong about the above post; it seems to cruise past a few lines where you use myModelTrf without issue, before triggering the NRE on line 74.

    That draws my suspicions to myModelTrf1, which appears to be an array that never gets initialized; additionally you seem to be using it like a dictionary or a list, rather than an array. Arrays can't be resized on the fly when you add new things. Try using a List<GameObject> instead of a GameObject[]. Make sure you initialize it in Awake or Start ( = new List<GameObject>() ), and to add to it, use .Add(myModelTrf)
     
    wahahahaharu and monodokimes like this.
  4. wahahahaharu

    wahahahaharu

    Joined:
    Mar 13, 2017
    Posts:
    2
    Thanks for reply StarManta!
    I simply initiate the array at the same time I declare it. And it works!
    As I just use objects to make a word, I don't need to add new things in the future.Using array is OK.
    (BTW I can't declare and initiate the list)

    Here comes another problem.
    I know it's so stupid but I can't fix it.

    Unity keeps saying that: Array index is out of range. But I really can't find any problem.

    Code (CSharp):
    1.  public Transform myModelPrefab;
    2.     public Transform[] myModelTrf = new Transform[40] ;
    3.     public GameObject[] pointers; //pointers size is 40
    4.     public bool created = false;
    5.     public float speed;
    6.     private int count = 0;
    and here are where the error comes:

    Code (CSharp):
    1.  
    2. public void OnTrackableStateChanged(
    3.               TrackableBehaviour.Status previousStatus,
    4.               TrackableBehaviour.Status newStatus)
    5.     {
    6.         if (newStatus == TrackableBehaviour.Status.DETECTED ||
    7.             newStatus == TrackableBehaviour.Status.TRACKED)
    8.         {
    9.             if(count < 40) {
    10.                
    11.                 Debug.Log(count);
    12.                 OnTrackingFound();    //here's the error line
    13.             }
    14.      
    15.         }
    16.     }
    17.     private void OnTrackingFound()
    18.     {
    19.         Debug.Log("Enter Found fun");
    20.         if (myModelPrefab != null)
    21.         {
    22.        
    23.             myModelTrf[count] = GameObject.Instantiate(myModelPrefab) as Transform  ;
    24.             Debug.Log("Created");
    25.             myModelTrf[count].parent = mTrackableBehaviour.transform;  //here's another error line
    26.             myModelTrf[count].localPosition = new Vector3(0f, 0.14f, 0f);
    27.             myModelTrf[count].localRotation = Quaternion.identity;
    28.             myModelTrf[count].localScale = new Vector3(0.0005f, 0.0005f, 0.0005f);
    29.  
    30.        
    31.             myModelTrf[count].gameObject.active = true;
    32.             created = true;
    33.             count++;
    34.         }
    35.  
    36. }
     
    Last edited: Apr 28, 2017