- Home /
Having problems destroying an Object
OK, so Im trying to destroy an object and cant seem to work out why its not happening. Basically Im at a terminal and want to switch a fence from activated to deactivated. Simply I want to destroy one object and spawn another in its place.
My script-
#pragma strict
var pickUp : AudioClip;
var lockedScreen : GameObject;
var unlockedScreen : GameObject;
var laserGateOpen : GameObject;
var laserGateClosed : GameObject;
function Start ()
{
}
function OnTriggerStay (other : Collider)
{
// ... and the switch button is pressed...
if (Input.GetKeyDown("i"))
{
// ... deactivate the laser.
AudioSource.PlayClipAtPoint(pickUp, transform.position);
Destroy(gameObject);
Instantiate(unlockedScreen, lockedScreen.transform.position, Quaternion.identity);
LaserFenceDeactivation();
}
}
function LaserFenceDeactivation ()
{
DestroyImmediate(laserGateClosed, true);
Instantiate(laserGateOpen, laserGateClosed.transform.position, Quaternion.identity);
}
At first I simply used Destroy(laserGateClosed) but got this error message-
Destroying assets is not permitted to avoid data loss.
If you really want to remove an asset use DestroyImmediate (theObject, true);
UnityEngine.Object:Destroy(Object)
LaserFenceControl:LaserFenceDeactivation() (at Assets/Scripts/LaserFenceControl.js:34)
LaserFenceControl:OnTriggerStay(Collider) (at Assets/Scripts/LaserFenceControl.js:27)
But if I use DestroyImmediate I get this error -
Destroying GameObjects immediately is not permitted during physics trigger/contact or animation event callbacks. You must use Destroy instead.
UnityEngine.Object:DestroyImmediate(Object, Boolean)
LaserFenceControl:LaserFenceDeactivation() (at Assets/Scripts/LaserFenceControl.js:34)
LaserFenceControl:OnTriggerStay(Collider) (at Assets/Scripts/LaserFenceControl.js:27)
Maybe Im missing something but one seems to contradict the other ?
Any ideas plz guys ???
Answer by iwaldrop · Jul 15, 2013 at 11:23 PM
You aren't working on instances of your Prefabs, but on the Prefab Originals themselves. @Supercouge has a good suggestion when he mentions to create the instances you are going to work with up front, and I concur. However, I wouldn't be destroying and instantiating things over and over again; just set them to inactive.
GameObject fieldActiveGO;
GameObject fieldInactiveGO;
function Start()
{
fieldActiveGO = Instantiate(fieldActivePrefab, transform.position, transform.rotation);
fieldInactiveGO = Instantiate(field=InactivePrefab, transform.position, transform.rotation);
}
function ChangeFieldState(bool turnOn)
{
fieldActiveGO.setActive(turnOn);
fieldInactiveGO.setActive(!turnOn);
}
Creating and destroying objects is very slow, so avoid it when you can. In the case of flipping things back and forth between two states, obviously, you can just turn one on and the other off.
Ive not used setActive yet. Will that still work with objects/instances that use triggers and animations?
An object which uses Trigger and Animation can, of course, be activated and desactivated. However, when an object is desactivated, it will stop to collide with other objects and it will not be animated anymore (neither rendered), until it is re-activated.
You should implement the solution of iwaldrop, it is the best way to achieve what you want to do.
GameObject fieldActiveGO; GameObject fieldInactiveGO;
Are what exactly? Ive assumed they are meant to be(In the case of my script)-
GameObject laserGateClosed; GameObject laserGateOpen;
However this just gives me another error-
Assets/Scripts/LaserFenceControl.js(10,11): UCE0001: ';' expected. Insert a semicolon at the end.
(If it makes any difference Im using Unity 4.1. Only saying this as when I tried to use one of the other suggestions by you guys I was getting an Obsolete message)
???
Did you figure it out, or are you still getting an error?
Still no luck with this. Seem to get a new error message each time I try something
Answer by supercouge · Jul 15, 2013 at 09:33 PM
I guess that:
var laserGateOpen : GameObject;
var laserGateClosed : GameObject;
Are directly linked to your Prefab and not to an instantiated prefab (or clone Prefab). It is usually better to work with instantiated prefab. It allow you, for example, to destroy the "Clone" by using Destroy(), anywhere you want in your script.
So in your Start() function, you should do something like this:
laserGateClosed = Instantiate(laserGateClosed, transform.position, Quaternion.identity);
laserGateOpen = Instantiate(laserGateOpen, transform.position, Quaternion.identity);
(you should do it for each of your prefab in fact, and each time you Instantiate a gameObject from a prefab). This way you will hold a clone object of your prefab, and, when you will call Destroy(), you will destroy the clone and not the prefab.
O$$anonymous$$.. were so close to getting this (I am that is. $$anonymous$$ost of you guys already know it...lol)
Script now reads -
#pragma strict
var pickUp : AudioClip;
var lockedScreen : GameObject;
var unlockedScreen : GameObject;
var laserGateOpen : GameObject;
var laserGateClosed : GameObject;
var LaserGateSpawner : GameObject;
function Start ()
{
laserGateClosed = Instantiate(laserGateClosed, LaserGateSpawner.transform.position, Quaternion.identity);
}
function OnTriggerStay (other : Collider)
{
// ... and the switch button is pressed...
if (Input.Get$$anonymous$$eyDown("i"))
{
// ... deactivate the laser.
AudioSource.PlayClipAtPoint(pickUp, transform.position);
Destroy(gameObject);
Instantiate(unlockedScreen, lockedScreen.transform.position, Quaternion.identity);
LaserFenceDeactivation();
}
}
function LaserFenceDeactivation ()
{
Destroy(laserGateClosed);
Instantiate(laserGateOpen, laserGateClosed.transform.position, Quaternion.identity);
}
Everything is working fine apart from one small thing. The LaserGateOpen is spawning(Instantiating) in the right place BUT its twisted 90 degrees on the Y axis. Ive tried changing the prefabs rotation, but that does nothing, still facing wrong way?
Shouldn't this line-
Instantiate(laserGateOpen, laserGateClosed.transform.position, Quaternion.identity);
Spawn the OpenGate at exactly the same place AND rotation of the gate its replacing ???