Why is Destroy(this.gameObject) not working immediately?

HI!
So I am trying to delete a gameObject being instantiated by including a script to it.
And at the same point I am instantiating another game object using different script.

My instantiating script works fine. But my Destroy is working slowly. The more the time the game runs, the more is the time it takes to destroy the game object.

Someone please help me by explaining whats happening and how to rectify it.
If this is not rectifiable, then give an optimal solution to solve my purpose.

Thanks in advance :slight_smile: Cheers! :slight_smile:

How fast are the frames running in your game? could you post the code your working with and tell us what you want it to do it could just be that your instantiating to many objects and things are starting to glitch, but i couldn’t say for sure.

There are few things I don’t get, firstly you say everything work but slowly, then in your code you write:

It din work even for
Destroy(this.gameObject);

, so it destroys them or not? Also the other things I’ll note below, but overall here is the thing:

  1. Do all of your 4 “ch” GameObjects contain “ObjectDestroy” script or script that inherits it? If yes then great if not then surely your code is not going to work.

  2. You have only 1 instance of “inst” class in whole gameworld? Most likely yes, because otherwise it would make no sense.

  3. I have no idea what your “GameOver.settimes” does, if it was just 1,5f I’d think of it as a call to load another scene or stop the spawn and show some scorer or something, but in your code it as well passes current time, so I have no idea.

  4. Why do you use int for desBg instead of bool?

Ok enough of questions.

Now back to your questions, if you want to “remove” all “ch” objects at once you have generally two ways to achieve that:

  1. Going the way you already went, and moreover you have almost done it correctly.

First of all make sure that all your objects meet my first question above^.

Then in your ObjectDestroy scrip change Update to this:

void Update()
{
    if (desBg == 1)
    {
        Destroy(gameObject);
        inst.count--;
    }
}

In your “inst” class change Update to this:

void Update()
{
    if (ObjectDestroy.desBg == 0)
    {
        if (Time.time >= tym || count == 0) ObjectDestroy.desBg = 1;
    }
    else if (count == 0)
    {
        tym = Time.time;
        spawnChars();
    }
}

And that should do it if I haven’t missed anything.

  1. Store all instance references.

First you’ll need to remove Update from ObjectDestroy script and all lines containing desBg variable.

Then start adding this to your “inst” script:

private List<GameObject> objRef;
//then in all Instantiate calls do like this:
objRef.Add(Instantiate(bla bla bla) as GameObject);
//then change your Update function to this:
void Update()
{
    if ((Time.time >= tym) || (count == 0))
    {
        for (int i = objRef.Count - 1; i >= 0; i--)
        {
            Destroy(objRef*);*

objRef.RemoveAt(i);
}
tym = Time.time;
spawnChars();
}
}
The second one theoretically should be faster.