Hi when I try to destroy an object in game it gives an error saying prefabs can’t be destroyed due to data loss. Help me please. I am also using vuforia for spawning my objects.
public void OnTrackableStateChanged(
TrackableBehaviour.Status previousStatus,
TrackableBehaviour.Status newStatus)
{
if (newStatus == TrackableBehaviour.Status.DETECTED ||
newStatus == TrackableBehaviour.Status.TRACKED ||
newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED) {
Debug.Log ("Spawned");
int spawnPointIndex = Random.Range (0, spawnPoints.Length);
// Create an instance of the enemy prefab at the randomly selected spawn point's position and rotation.
Instantiate (Monster, spawnPoints [spawnPointIndex].position, spawnPoints [spawnPointIndex].rotation);
}
else
{
Destroy(Monster); }
}
}
I guess what we’re trying to figure out, (to help you) is what exactly are you trying to do with this script?
Looks here you’re trying to destroy “Monster” in the else statement, without having instanced a clone of that prefab. Typically when you’re using prefabs to create game objects in your game you want to make an instance of that prefab, which then essentially creates the game object as a copy of the prefab you’ve made before.
The main issue I can see in your current script is that your trying to destroy something in an else statement that hasnt been created in a previous part of the script (or at least one in a scope that would occur that is not mutually exclusive due to if statement). Essentially you can not destroy something that does not exist in your game (has not been instantiated).
I think I see what’s going on here. You’re using the Vuforia SDK. You want a Monster to show when the marker is tracked, and you want it to disappear when the image is not.
I’d suggest instancing the monster right away, and hiding it when the marker is hidden:
Thank you guys so much. However I am trying to build a yugioh game and I am using vuforia. However I have a question when you mean by hiding it does the rest of the object like colliders and scripts still present or is it just the mesh thats hidden. Thanks
using UnityEngine;
namespace Vuforia
{
public class Spawn : MonoBehaviour,
ITrackableEventHandler
{
private TrackableBehaviour mTrackableBehaviour;
public GameObject Monster; // The enemy prefab to be spawned.
public Transform[] spawnPoints;
public Transform Zone1L;
public Transform Zone2L;
public Transform Zone3L;
public Transform Zone4L;
public Transform Zone5L;
public Collider Zone1;
public Collider Zone2;
public Collider Zone3;
public Collider Zone4;
public Collider Zone5;
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) {
Debug.Log ("Spawned");
int spawnPointIndex = Random.Range (0, spawnPoints.Length);
// Create an instance of the enemy prefab at the randomly selected spawn point's position and rotation.
Instantiate (Monster, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
else
{
Debug.Log("gone");
Destroy(Monster);
}
}
}
}
I’d suggest you familiarize yourself with the Unity basics. Posting to the forums is fine, but you’ll find our assistance much more useful once you’ve got an understanding of the fundamentals.
Maybe You guys can help me with this piece of code.All I want is If the Trackable Behavior is detected I want a object to be spawned in another spot if an object is in the intended spot. And I was thinking to use colliders as a reference as to know if a object is in the spot or not.
using UnityEngine;
namespace Vuforia
{
public class Spawn : MonoBehaviour,
ITrackableEventHandler
{
private TrackableBehaviour mTrackableBehaviour;
public GameObject Monster; // The enemy prefab to be spawned.
public Vector3 Zone1V;
public Vector3 Zone2V;
public Vector3 Zone3V;
public Vector3 Zone4V;
public Vector3 Zone5V;
public Collider Zone1;
public Collider Zone2;
public Collider Zone3;
public Collider Zone4;
public Collider Zone5;
GameObject SpawnedM;
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)
{
SpawnedM = Instantiate (Monster, Zone1V, Quaternion.identity) as GameObject;
Debug.Log ("Monster Summoned");
}
else
{
Destroy(SpawnedM);
Debug.Log ("Monster Destroyed");
}
}
}
}