我不知道出了什么问题。调试日志说
NullReferenceException: Object reference not set to an instance of an object
Vuforia.ZoneManagerScript.Spawn () (at Assets/Yugioh/ZoneManagerScript.cs:31)
Vuforia.Spawn.OnTrackableStateChanged (Status previousStatus, Status newStatus) (at Assets/Yugioh/Spawn.cs:47)
Vuforia.TrackableBehaviour.OnTrackerUpdate (Status newStatus)
Vuforia.DataSetTrackableBehaviour.OnTrackerUpdate (Status newStatus)
Vuforia.StateManagerImpl.UpdateTrackablePoses (UnityEngine.Transform arCameraTransform, .TrackableResultData[] trackableResultDataArray, Int32 originTrackableID, Int32 frameIndex)
Vuforia.VuforiaManagerImpl.UpdateTrackers (FrameState frameState)
Vuforia.VuforiaManagerImpl.Update (ScreenOrientation counterRotation, Boolean reapplyOldState)
Vuforia.VuforiaAbstractBehaviour.UpdateStatePrivate (Boolean forceUpdate, Boolean reapplyOldState)
Vuforia.VuforiaAbstractBehaviour.Update ()".
相关代码片段:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
namespace Vuforia
{
public class ZoneManagerScript : MonoBehaviour
{
public Vector3 Zone1L;
public Vector3 Zone2L;
public Vector3 Zone3L;
public Vector3 Zone4L;
public Vector3 Zone5L;
GameObject SpawnedM;
Spawn SpawnScript;
GameObject monster;
public void Spawn ()
{
GameObject MonsterONField = GameObject.FindGameObjectWithTag ("Monster");
SpawnScript = MonsterONField.GetComponent<Spawn>();
monster = SpawnScript.Monster;
Dictionary <Vector3,bool> IsZoneEmpty = new Dictionary <Vector3,bool>();
IsZoneEmpty.Add(Zone1L,true);
if (IsZoneEmpty [Zone1L] == true)
{
SpawnedM = Instantiate(monster,Zone1L,Quaternion.identity) as GameObject;
Debug.Log("MonsterSpawned");
IsZoneEmpty[Zone1L]=false;
}
}
}
}
片段2:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
namespace Vuforia
{
public class Spawn : MonoBehaviour,
ITrackableEventHandler
{
private TrackableBehaviour mTrackableBehaviour;
public GameObject Monster; // The enemy prefab to be spawned.
GameObject Zonemanager;
ZoneManagerScript ZMS;
void Start()
{
mTrackableBehaviour = GetComponent<TrackableBehaviour> ();
if (mTrackableBehaviour)
{
mTrackableBehaviour.RegisterTrackableEventHandler (this);
}
}
public void OnTrackableStateChanged(
TrackableBehaviour.Status previousStatus,
TrackableBehaviour.Status newStatus)
{
if (newStatus == TrackableBehaviour.Status.DETECTED ||
newStatus == TrackableBehaviour.Status.TRACKED ||
newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
{
Zonemanager = GameObject.FindGameObjectWithTag ("Zone Manager");
ZMS = Zonemanager.GetComponent<ZoneManagerScript> ();
ZMS.Spawn();
}
else
{
}
}
}
}