Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Destroying Cloned Prefabs

Discussion in 'Scripting' started by DanSingh, Aug 13, 2015.

  1. DanSingh

    DanSingh

    Joined:
    Feb 5, 2015
    Posts:
    98
    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.
    Code (CSharp):
    1. public void OnTrackableStateChanged(
    2.             TrackableBehaviour.Status previousStatus,
    3.             TrackableBehaviour.Status newStatus)
    4.         {
    5.             if (newStatus == TrackableBehaviour.Status.DETECTED ||
    6.                 newStatus == TrackableBehaviour.Status.TRACKED ||
    7.                 newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED) {
    8.                 Debug.Log ("Spawned");
    9.                 int spawnPointIndex = Random.Range (0, spawnPoints.Length);
    10.                        
    11.                 // Create an instance of the enemy prefab at the randomly selected spawn point's position and rotation.
    12.                   Instantiate (Monster, spawnPoints [spawnPointIndex].position, spawnPoints [spawnPointIndex].rotation);
    13.             }
    14.             else
    15.             {
    16.                 Destroy(Monster);            }
    17.         }
    18.                
    19.             }
     
  2. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    assign the result of Instantiate() to a variable. That variable represents the instance you created. Destroy that instead.
    Code (CSharp):
    1. var instance = Instantiate(prefab);
    2. Destroy(instance);
    3.  
     
  3. DanSingh

    DanSingh

    Joined:
    Feb 5, 2015
    Posts:
    98
    It won't let me put instance in destroy command
     
  4. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    Try :
    Code (CSharp):
    1. GameObject clone = (GameObject)Instantiate (Monster, spawnPoints [spawnPointIndex].position, spawnPoints [spawnPointIndex].rotation);
    2. Destroy(clone);
     
  5. DanSingh

    DanSingh

    Joined:
    Feb 5, 2015
    Posts:
    98
    It still doesn't Work.
     
  6. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    Try declaring the "clone" variable at the top of your script.
     
  7. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    Why are you trying to destroy it if it wasn't instanced to begin with?
     
  8. CastleIsGreat

    CastleIsGreat

    Joined:
    Nov 10, 2014
    Posts:
    176
    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).
     
    BenZed likes this.
  9. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    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:


    Code (CSharp):
    1. GameObject monster;
    2.  
    3. void Start()
    4. {
    5.     monster = Instantiate<GameObject>(MonsterPrefab);
    6. }
    7.  
    8. void OnTrackableStateChanged(Status previousStatus, Status newStatus)
    9. {
    10.     bool visible = newStatus == TrackableBehaviour.Status.DETECTED ||
    11.                       newStatus == TrackableBehaviour.Status.TRACKED ||
    12.                    newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED;
    13.                  
    14.     monster.SetActive(visible);
    15.  
    16. }
     
  10. DanSingh

    DanSingh

    Joined:
    Feb 5, 2015
    Posts:
    98
    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
     
  11. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
  12. DanSingh

    DanSingh

    Joined:
    Feb 5, 2015
    Posts:
    98
    I will post the whole script.
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. namespace Vuforia
    4. {
    5.     public class Spawn : MonoBehaviour,
    6.     ITrackableEventHandler
    7.     {
    8.  
    9.        
    10.         private TrackableBehaviour mTrackableBehaviour;
    11.         public GameObject Monster;                // The enemy prefab to be spawned.
    12.         public Transform[] spawnPoints;
    13.         public Transform Zone1L;  
    14.         public Transform Zone2L;
    15.         public Transform Zone3L;
    16.         public Transform Zone4L;
    17.         public Transform Zone5L;
    18.  
    19.         public Collider Zone1;
    20.         public Collider Zone2;
    21.         public Collider Zone3;
    22.         public Collider Zone4;
    23.         public Collider Zone5;
    24.        
    25.  
    26.         void Start()
    27.         {
    28.             mTrackableBehaviour = GetComponent<TrackableBehaviour>();
    29.             if (mTrackableBehaviour)
    30.             {
    31.                 mTrackableBehaviour.RegisterTrackableEventHandler(this);
    32.             }
    33.  
    34.            
    35.         }
    36.        
    37.  
    38.         public void OnTrackableStateChanged(
    39.             TrackableBehaviour.Status previousStatus,
    40.             TrackableBehaviour.Status newStatus)
    41.         {
    42.             if (newStatus == TrackableBehaviour.Status.DETECTED ||
    43.                 newStatus == TrackableBehaviour.Status.TRACKED ||
    44.                 newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED) {
    45.                 Debug.Log ("Spawned");
    46.                 int spawnPointIndex = Random.Range (0, spawnPoints.Length);
    47.                
    48.                 // Create an instance of the enemy prefab at the randomly selected spawn point's position and rotation.
    49.                 Instantiate (Monster, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
    50.             else
    51.             {
    52.                 Debug.Log("gone");
    53.                 Destroy(Monster);
    54.             }
    55.                
    56.             }
    57.        
    58.         }
    59.        
    60. }    
     
  13. DanSingh

    DanSingh

    Joined:
    Feb 5, 2015
    Posts:
    98
  14. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    Yes! Did you read the link I sent you?

    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.
     
  15. DanSingh

    DanSingh

    Joined:
    Feb 5, 2015
    Posts:
    98
    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.
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. namespace Vuforia
    4. {
    5.     public class Spawn : MonoBehaviour,
    6.     ITrackableEventHandler
    7.  
    8.     {
    9.  
    10.        
    11.         private TrackableBehaviour mTrackableBehaviour;
    12.         public GameObject Monster;                // The enemy prefab to be spawned.
    13.  
    14.         public Vector3 Zone1V;
    15.         public Vector3 Zone2V;
    16.         public Vector3 Zone3V;
    17.         public Vector3 Zone4V;
    18.         public Vector3 Zone5V;
    19.  
    20.         public Collider Zone1;
    21.         public Collider Zone2;
    22.         public Collider Zone3;
    23.         public Collider Zone4;
    24.         public Collider Zone5;
    25.         GameObject SpawnedM;
    26.  
    27.  
    28.         void Start()
    29.         {
    30.             mTrackableBehaviour = GetComponent<TrackableBehaviour>();
    31.             if (mTrackableBehaviour)
    32.             {
    33.                 mTrackableBehaviour.RegisterTrackableEventHandler (this);
    34.  
    35.             }
    36.         }
    37.         public void OnTrackableStateChanged(
    38.             TrackableBehaviour.Status previousStatus,
    39.             TrackableBehaviour.Status newStatus)
    40.  
    41.         {
    42.         if (newStatus == TrackableBehaviour.Status.DETECTED ||
    43.             newStatus == TrackableBehaviour.Status.TRACKED ||
    44.             newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
    45.                 {
    46.                     SpawnedM = Instantiate (Monster, Zone1V, Quaternion.identity) as GameObject;
    47.                     Debug.Log ("Monster Summoned");
    48.                 }
    49.             else
    50.                 {
    51.                     Destroy(SpawnedM);
    52.                     Debug.Log ("Monster Destroyed");
    53.                 }
    54.             }
    55.         }
    56.     }
    57.  
    58.    
    Help.