2015-12-29 26 views
-1

我發佈Quiz.cs腳本,我選擇隨機遊戲對象。現在在DefaultTrackableEventHandler中,我需要檢查隨機選擇的遊戲對象的ImageTarget是否等於相機顯示的ImageTarget!如何檢查當前顯示的ImageTarget是否等於隨機選擇的ImageTarget?

using UnityEngine; 
using System.Collections; 
using System.Collections.Generic; 
using Vuforia; 

public class Quiz : MonoBehaviour 
{ 

    public GameObject[] models; 
    public GameObject currentPoint; 
    int index; 
    public AudioSource correctAudio; 
    public AudioSource notcorrectAudio; 
    //QuizLogic test ; 
    //public ImageTargetBehaviour checck; 
    //private TrackableBehaviour mTrackable; 

    //public GameObject[] sounds; 
    void Start() 
    { 

     StateManager sm = TrackerManager.Instance.GetStateManager(); 
     IEnumerable<TrackableBehaviour> activeTrackables = sm.GetActiveTrackableBehaviours(); 

     models = GameObject.FindGameObjectsWithTag("numbers"); 
     index = Random.Range (0, models.Length); 
     currentPoint = models[index]; 
     print ("Trackable " +currentPoint.name); 
     currentPoint.GetComponent<AudioSource>().Play(); 
     foreach (TrackableBehaviour tb in activeTrackables) 
     { 
      // As you iterate, you compare with current game object name 
      // if you have 3 and 5 and target object is 5 then you get a match. 
      if(tb.GetComponent<ImageTargetBehaviour>().ImageTarget.Name==currentPoint.GetComponent<ImageTargetBehaviour>().ImageTarget.Name) 
      { 
       print ("Congratulations you have chosen the correct ImageTarget"); 
      } 
      else 
      { 
       print ("Try another one !"); 
      } 
     } 
} 
} 

現在,當我把這個腳本放到OnTrackingFound()在DefaultTrackableEventHandler.cs(當我展示給相機一個ImageTarget它與任意選擇ImageTarget比較)我得到這個錯誤: 的NullReferenceException:對象引用未設置爲一個對象

這個實例是DefaultTrackableEventHandler.cs:

using UnityEngine; 

namespace Vuforia 
{ 
    /// <summary> 
    /// A custom handler that implements the ITrackableEventHandler interface. 
    /// </summary> 
    public class QuizLogic : MonoBehaviour, 
    ITrackableEventHandler 
    { 
     #region PRIVATE_MEMBER_VARIABLES 

     private TrackableBehaviour mTrackableBehaviour; 


     #endregion // PRIVATE_MEMBER_VARIABLES 


     Quiz quiz; 



     #region UNTIY_MONOBEHAVIOUR_METHODS 

     void Start() 
     { 

      mTrackableBehaviour = GetComponent<TrackableBehaviour>(); 
      if (mTrackableBehaviour) 
      { 
       mTrackableBehaviour.RegisterTrackableEventHandler(this); 
      } 
     } 

     #endregion // UNTIY_MONOBEHAVIOUR_METHODS 



     #region PUBLIC_METHODS 
     public GameObject show; 
     public GameObject hide; 

     /// <summary> 
     /// Implementation of the ITrackableEventHandler function called when the 
     /// tracking state changes. 
     /// </summary> 
     public void OnTrackableStateChanged(
      TrackableBehaviour.Status previousStatus, 
      TrackableBehaviour.Status newStatus) 
     { 
      if (newStatus == TrackableBehaviour.Status.DETECTED || 
       newStatus == TrackableBehaviour.Status.TRACKED || 
       newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED) 
      { 
       OnTrackingFound(); 


      } 

      else 
      { 
       OnTrackingLost(); 

      } 
     } 

     #endregion // PUBLIC_METHODS 



     #region PRIVATE_METHODS 


     private void OnTrackingFound() 
     { 

      show.SetActive(true); 
      Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true); 
      Collider[] colliderComponents = GetComponentsInChildren<Collider>(true); 
      AudioSource[] audiocomponents = GetComponentsInChildren<AudioSource>(true); 

      // Enable rendering: 
      foreach (Renderer component in rendererComponents) 
      { 
       component.enabled = true; 
      } 


      // Enable colliders: 
      foreach (Collider component in colliderComponents) 
      { 
       component.enabled = true; 
      } 

      //Enable AudioSource 
      foreach (AudioSource component in audiocomponents) 
      { 
       component.enabled = true; 
      } 

      Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " found"); 
      quiz.GetComponent<Quiz>(); 

     } 


     private void OnTrackingLost() 
     { 
      hide.SetActive(true); 
      Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true); 
      Collider[] colliderComponents = GetComponentsInChildren<Collider>(true); 
      AudioSource[] audiocomponents = GetComponentsInChildren<AudioSource>(true); 
      // Disable rendering: 
      foreach (Renderer component in rendererComponents) 
      { 
       component.enabled = false; 
      } 

      // Disable colliders: 
      foreach (Collider component in colliderComponents) 
      { 
       component.enabled = false; 
      } 


      //Disable AudioSource 
      //foreach (AudioSource component in audiocomponents) 
      //{ 
      // component.enabled = false; 
      //} 



      Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " lost"); 
     } 



     #endregion // PRIVATE_METHODS 
    } 
} 

請幫我,因爲我是新來的Vuforia!我將不勝感激,如果你的幫助

回答

0

你有問題是NullReferenceException。在這種情況下,你想達到的目標幾乎毫不相關。

只需檢查NullReferenceException發生在哪一行代碼中,找到未分配的引用(在該行中),確保它被正確分配並且該問題將被解決。

+0

這是給出錯誤的行: quiz.GetComponent (); 在這一行我打算在Default trackableEventHandler,OnTrackingFound()函數中調用測驗腳本 – Butrint

+0

這行有兩個問題。首先:'quiz'永遠不會被分配一個值,因此它是空的,這意味着當你嘗試調用它的一個成員時(這是發生在你身上的事情)你會得到一個NullReferenceException。第二行:'quiz.GetComponent ();'沒有效果。 'GetComponent'只返回組件(如果存在),它不會對它做任何事情。 –

+0

我想要做的是檢查OnTrackingFound()函數,如果從Quiz.cs中提取的隨機選擇的對象與向攝像機顯示的對象相同。你能幫我做這個嗎?我會很感激 – Butrint

相關問題