Object not Deleting

private var wallType : int;
var wall : Transform;

function Start () {
wallType = Random.Range(1,4);
}

function Update () {
if(wallType == 1){
	Instantiate(wall, transform.position, transform.rotation);
	Destroy(gameObject);
}
}

It works, but every once in a while the object doesnt delete itself at the end.

Help??

Hi :slight_smile: maybe I dont understand what you want from script to do.
Well so : (if i get it) :smile:
it dont destroy created wall because Destroy(gameObject); destroy object that script is attached to.

EDIT : Oh and if your wallType is something else then 1, update function dont destroy that object script is attached too because destroy is in IF statement and it has to be 1 to execute.

you maybe want to check this too :

    // Kills the game object
    Destroy (gameObject);
    // Removes this script instance from the game object
    Destroy (this);
    // Removes the rigidbody from the game object
    Destroy (rigidbody);

http://docs.unity3d.com/Documentation/ScriptReference/Object.Destroy.html
hope it helps :wink: cheers

To clarify, I meant it sometimes doesnt destroy, even if wallType = 1.

I dont know why i copy your script and add Debug.Log ( wallType );
and it works great

Try removing the if statement just to ensure it’s not the WallType.

Ok, just so we’re all on the same page, I start with two walls that are children of a single empty gameobject. That gameobject has the above script attached. It looks like so:

If “wallType” = 1, it should delete that gameobject (the two walls included) and create a solid wall, like this:

But sometimes, it does not delete itself, but still creates the wall, resulting in this:

Hopefully that explains the issue more clearly.

Did you check that none of the wall/s you are instantiating have no script which may have a weird side effect? I am actually sure that something else in your project causes this to happen.

Another question: Why do you instantiate the wall in update and not already in start?

private var wallType : int;
var wall : Transform;
     
function Start () {
    wallType = Random.Range(1,4);
    if(wallType == 1){
        Instantiate(wall, transform.position, transform.rotation);
        Destroy(gameObject);
    }
}

Thats what i had thought too, but the wall object being instantiated is nothing but a cube with a texture. no scripts at all.

So if it is not being destroyed, you should be able to reproduce it somehow. You could place a Debug.Log before the Destroy and a Debug.Log after the destruction to check if that is really the problem.

not sure I understand… why would it possibly create the wall but not destroy itself if both actions are in the same if statement?

This can certainly not happen. I actually meant it differently and confused even myself :slight_smile: .
My actual question is, do you have more code in your actual project and show us only snippets? Is anything else happening in the Update function? Probably yes, otherwise you wouldn’t have to place the destruction there.

As far as this particular script? no.

I tried putting it in the start function. The same problem still occurs.

I fear you have to share your project or parts of it. It is pretty obvious that other scripts produce that issue. E.g. the Prefab you instantiate has that script in a child object. Or the intersecting walls are produces by another game object that has this script as well… That is way too much guessing for such a simple script.