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.

You can only remove GameObjects, Transform is a component of a GameObject. There many docs around about Componet Base development, like Unity - Manual: Use components.

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.

Second try :stuck_out_tongue:

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.