Log in or register to post comments

Removing an Instantiated Prefab from a scene

January 18, 2016 - 3:56pm #1

Is is possible to remove an instantiated prefab from a unity scene as part of the "tracking lost" function of the DefaultTrackableEventHandler.

I have mutlple image targets loaded into a single scene and I want to load prefabs at runtime when OnTrackingFound() and unload the prefab when OnTrackingLost()


I am loading prefabs from the Resources folder and want to remove them when the tracking is lost. The reason from removing them is to save memory within the scene.


loading the prefab has not been a probelm just unloading and removing it from the scene.


public GameObject Model; (I manually drag the prefab to the inspector to specify the prefab to load)


-- Model is loaded and made child of Imagetarget short script from function OnTrackingFound()

GameObject Show = ((GameObject)Instantiate(Model)).gameObject;

            Show.transform.parent = transform;


Here is the tricky part, how to remove the Model prefab that has become gameobject "show" and parented itself to the Imagetarget. I need to do this without messing up the other Imagetargets within the same scene. Although I am only trying to track one target at a time.

Short script from function OnTrackingLost()

public GameObject Model;

{ GameObject Show = Model.gameObject;
            Model.transform.parent = null;

        if (Show.gameObject.name == Model.name) {
                Destroy (Show.gameObject);}


Error message returned within Unity: 


Destroying assets is not permitted to avoid data loss.
If you really want to remove an asset use DestroyImmediate (theObject, true);
Vuforia.DefaultTrackableEventHandler:OnTrackingLost() (at Assets/Vuforia/Scripts/DefaultTrackableEventHandler.cs:110)
Vuforia.DefaultTrackableEventHandler:OnTrackableStateChanged(Status, Status) (at Assets/Vuforia/Scripts/DefaultTrackableEventHandler.cs:65)
Vuforia.StateManagerImpl:UpdateTrackablePoses(Transform, TrackableResultData[], Int32, Int32)
Vuforia.VuforiaManagerImpl:Update(ScreenOrientation, Boolean)
Vuforia.VuforiaAbstractBehaviour:UpdateStatePrivate(Boolean, Boolean)

Any thoughts on best way to remove the prefab that was instantiated. 



Removing an Instantiated Prefab from a scene

January 19, 2016 - 7:48am #2

Sounds more like a Unity programming question; for example, this Unity forum thread discusses Asset / prefabs destruction:



Log in or register to post comments