- Home /
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 :) Cheers! :)
Read this. If your delay is greater than that then post the code where and how you call Destroy.
Hi :) Just now noticed your comment.
I have given a description about the process along with the code and my need. Please look at that and help me out.
Thanks in advance. :)
Answer by DoTA_KAMIKADzE · Apr 14, 2015 at 04:00 PM
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.
2) 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[i]);
objRef.RemoveAt(i);
}
tym = Time.time;
spawnChars();
}
}
The second one theoretically should be faster.
I am sorry that is a typo. I was about to type It din work properly :)
Consider there are 5 prefabs out of which 2 prefabs I have to delete after the time ends. And yes ObjectDestroy script is added to all the game objects which I want to get destroyed.
You guessed it right :) Only one inst.cs
Please don worry about that. It has nothing to do with this part.
Yes. $$anonymous$$y codes need a lot of optimization. :) btw. is bool in anyother ways effective or faster than int? If so please tell.
Thanks a ton for spending your time on this. :)
Will try the methods and will let you know about the update :)
The 2nd one is just working great! Din try the first one though :) Thanks for your kind help. Cheers :)
Please keep up your work. :)
And I have another query which I think you could answer. But this is in now way related to the above stuffs.
I want to stop a part of code from being executed after certain condition is reached. I remember using stuffs like break; in c++ but only breaks from the condition block which i use to end.
application.quit() is meant to solve my purpose. But it just wont work on unity editor(Thats what I have been told). This is the same inst.cs Update() funciton
void Update () { if ((Time.time >= tym) || (count == 0)) { for (int i = objToDestroy.Count - 1; i >= 0; i--) { Destroy(objToDestroy[i]); objToDestroy.RemoveAt(i); }
if (count != 0) {
Debug.LogError ("Game Over");
Application.Quit();
}
//remaining part of code which I dont want to get executed.
spawnChars();
}
}
Even though I use debug.logError() this couldnt stop the remaining code getting executed. This stops only after executing the whole function block. Please explain me why this happens.
Help me in solving this. :)
As for bool vs int, which one will check faster depends on processor and system, in other words in most modern systems no difference in performance.
It's just more visually understandable and more prone to misstyping (especially if someone gets to your code or you get to it after few years). Also with bool you can do something like that:
if (!bool) //to mimic that with int (int != someInt)
if (bool) //to mimic that with int (int == someInt)
//and you can do that:
bool = !bool; //you can mimic that as well but only if int is not 0 then (-int)
P.S. For your update in comment:
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#else
Application.Quit();
#endif
Answer by J-R-Wood · Apr 14, 2015 at 12:39 AM
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.
Thanks for replying :) As you said I am instantiating too many objects at same positions Here is my code snippet which instantiates the game objects
public class inst : $$anonymous$$onoBehaviour {
public GameObject[] ch = new GameObject[4] ;
public Transform[] spawnpoint=new Transform[4];
public Transform[] spawnpoint1=new Transform[3];
public static float tym = 0;
public static int count = 0;
void Start () {
spawnChars ();
}
void spawnChars () {
int y =Random.Range (0,2);
if(y==0){
for(int i=0; i<4; i++){
Vector3 spawnVector = new Vector3 (spawnpoint[i].position.x, spawnpoint[i].position.y, spawnpoint[i].position.z);
int x = Random.Range (0,4);
if(ch[x].tag == "anims")
{++count;}
Instantiate (ch[x], spawnVector, spawnpoint[i].rotation);
}
tym+=1.5f;
GameOver.settimes(1.5f,Time.time);
}
else if(y==1){
for (int i=0; i<3; i++) {
Vector3 spawnVector = new Vector3 (spawnpoint1 [i].position.x, spawnpoint1 [i].position.y, spawnpoint1 [i].position.z);
int x = Random.Range (0, 4);
if(ch[x].tag == "anims")
++count;
Instantiate (ch [x], spawnVector, spawnpoint1 [i].rotation);
}
tym+=1.5f;
}
}
void Update () {
if ((Time.time >= tym)|| (count==0)) {
tym = Time.time ;
ObjectDestroy.desBg=1;
spawnChars();
}
}
}
And my code snippet for destroying which is attached to the prefab that vud be instantiated in previous code. public class ObjectDestroy : $$anonymous$$onoBehaviour { public static int desBg=0;
void Update () { if (desBg == 1) {
GameObject gm = this.gameObject;
Destroy(gm); // It din work even for Destroy(this.gameObject);
desBg =0 ;
}
}
Now, all I need is to destroy these instantiated clones immediately after the condition gets satisfied. if not possible, at least give a solution to make the gameObject invisible and then Destroy them later. Thanks once again :)
Your answer
Follow this Question
Related Questions
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
How to delay lines of code. 2 Answers
when instantiate object then game stop for some time 1 Answer
Delay "Destroy(gameObject)" 2 Answers
How to Destroy gameObjects and deal damage within a radius except player ? 2 Answers