- Home /
Destroy and Instantiate
I should precede my question by saying I am Unity newbie so am trying to learn many things with regards to it, but am unsure with an issue I am getting here and haven't been able to find a solution. It is using a game / code that had already been written by someone else in a previous version of Unity (not in contact with that person anymore and unsure on exact version). Previously this part of the game was working, but upon opening the project in the version 3.4.2 this aspect of the game does not work correctly now.
To get to the point of the problem, I have a jetfire that when it collides with a wall it creates another particle stream of fire and smoke rising. This still works fine currently, but when you now move the location of the wall instead of the fire and smoke that arose due to collision being destroyed it remains in place.
Originally, the code used the following
Destroy(instantiatedFire);
Destroy( instantiatedSmoke );
This in the new version of Unity gave the error message that it could not destroy a transform. Through searching through other similar questions asked I changed this code to the following:
Destroy( (instantiatedFire as Transform).gameObject );
Destroy ( (instantiatedSmoke as Transform).gameObject );
This instead of just destroying the instantiated fire and smoke when you moved the wall, as it should have done, it destroyed it to the extent that it wasn't even created even though there is code following it to instantiate the fire. Part of the following code here:
var instantiatedFireOrigin = hit.point + hit.normal*0.75;
instantiatedFire = Instantiate(radialFire, instantiatedFireOrigin, Quaternion.identity);
var rotationAxis : Vector3 = instantiatedFire.transform.TransformDirection(1, 0, 0);
instantiatedFire.transform.rotation = Quaternion.FromToRotation(rotationAxis, hit.normal);
Would be grateful for any feedback on this to hopefully try and sort this issue.
Please, write less and directly if you want a answer. Long histories scare most readers.
Answer by Sisso · Sep 14, 2012 at 11:42 AM
You can only remove GameObjects, Transform is a component of a GameObject. There many docs around about Componet Base development, like http://docs.unity3d.com/Documentation/Manual/UsingComponents.html.
I think that the problem is not inside these codes, you are almost there. Could be better create TestScene were you could test each code separated. You could debug using proper name, step foward and Debug.Log messages.
Thanks for feedback Sisso. Will take note for next time. Will check that documentation out. I had thought that it could be an issue outside of the code since there is a lifetime assigned to these particles which seems to be getting ignored as well.
I am getting a NullReferenceException error arising from the same line of destroy code, as referenced above, which is part of an if statement further on in the code, but don't know if that issue is related.
Answer by Sisso · Sep 14, 2012 at 02:25 PM
Second try :P
I understand that you instantiate many "radialFire" GameObject, but you store only the last reference. So, when you call Destroy(instantiatedFire), you are destroing only the last instance.
If was it, I see two fast solution.
1) Change your into an array, sample code:
// declare var
var instantiatedFires = new Array();
// create 10 objects
for (var i = 0; i < 10 ; i ++) {
var fire = Instantiate(radialFire);
instantiatedFires.add(fire);
}
// to delete all
for (var fire : GameObject in instantiatedFires) {
Destroy(fire);
}
Reference about arrays:
http://docs.unity3d.com/Documentation/ScriptReference/Array.html
2) Put a script inside your radialFire that destroy self when its needed.
Using that bit of code as a starting point I get "Unexpected token: &" and "expecting 'p'. found '6' " with reference to the declare var line. I'm not familiar with the use of "=" and "+" to identify a workaround here.
It's simple sample code, don't expect copy paste and it works :P
Schoolboy error on my part. Can't really use the not knowing the format of the site here as it should have been the first thing I checked. You are right that radialFire clones keep on getting instantiated when you move the wall so will work on this to be able to destroy all the previously called clones.
Thanks for your help.
I have had a look at this more and watched it more in the inspector. Only one radialFire transform (not game object) is created when you move the wall. If I put in any form of a Destroy command in treating the radialFire or instantiatedFire transforms as a gameObject it destroys it to the extent that it isn't even created despite the instantiate command being called after the Destroy command in the if statement. I can't understand why the radialFire component is not 'created' again here. I get a NullReferenceException on this line further on as well:
instantiatedFire.GetComponentInChildren(SandboxFireEvent).RunFireEvent();
Your answer
![](https://koobas.hobune.stream/wayback/20220613081034im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Bug: cannot assign public Transform variable. Unity 2D 5.2 1 Answer
How to turn object into a particle system and back? 0 Answers
Is "null" the best way to unparent a child? 1 Answer
Destroy vs Don't Destroy 2 Answers
Script problems... 1 Answer