- Home /
Only delete the gameobject ONCE
I want to destroy OBJ001 ONCE it's entered the 2000exp, how could i fix my script part to get it to that?
if(GM.enemy_exp >= 2000)
{
if(BuildSpots[0].tag == "TurretSpotClosed")
{
Destroy(OBJ001);//destroy the object only once
BuildSpots[0].tag = "TurretSpotOpen";
OBJ001 = Instantiate(Turrets[1], BuildSpots[0].position, Quaternion.identity)as GameObject;
BuildSpots[0].tag = "TurretSpotClosed";
}
}
else if(GM.enemy_exp >= 600)
{
if(BuildSpots[0].tag == "TurretSpotOpen")
{
OBJ001 = Instantiate(Turrets[0], BuildSpots[0].position, Quaternion.identity)as GameObject;
BuildSpots[0].tag = "TurretSpotClosed";
}
}
You can just make a bool variable and set it to false. Then, check if your exp is above 2000 and that bool is false. Set it to true and destroy it.
Easiest way out, at least in my opinion.
You could also start a coroutine that checks if Gm.enemy_exp reaches 2000. If it does, then destroy the object and end the coroutine.
Or you could control how G$$anonymous$$.enemy_exp is increased. Then if G$$anonymous$$.enemy_exp<2000
but G$$anonymous$$.enemy_exp+exp>=2000
set G$$anonymous$$.enemy_exp+=exp
and destroy the gameObject.
The problem is, i need to do that several time, so bool is not working i think.
Starting a coroutine is the same problem, i need to do this several times. No option for me i think. Still thanks.
Answer by Mark Gossage · Jun 24, 2015 at 03:48 AM
You might be able to use the name/tag of the OBJ001 to solve it. Otherwise, you will be creating/destroying objects like mad. I like revolute's idea of a bool, but since it seems that you have several levels, I would recommend an enum. I'm assuming this is some kind of tech-level advancement for an RTS or similar. So this is how I would approach it:
enum Tech{Level0,Level1,Level2};
Tech currentTech=Tech.Level0;
GameObject OBJ001=null;
...
if (currentTech==Tech.Level0 && GM.enemy_exp >= 600)
{
// create OBJ001
currentTech=Tech.Level1;
}
if (currentTech==Tech.Level1 && GM.enemy_exp >= 2000)
{
// delete old OBJ001
// create new OBJ001
currentTech=Tech.Level2;
}
// and so on.
This seems to be very solid. I'll give it a try, and give you feedback.
If i have 3 techlevels per "tier", can i use another enum to increase that tier too, and begin again at tech level 1? This would maybe safe me alot of coding. Or Should i just take an int and increase that for the "tier"?
Works pretty well!
if(currentTech == Tech.Level0 && G$$anonymous$$.enemy_exp>= 600)
{
OBJ001 = Instantiate(Turrets[0], BuildSpots[0].position, Quaternion.identity)as GameObject;
currentTech = Tech.Level1;
}
if(currentTech == Tech.Level1 && G$$anonymous$$.enemy_exp>= 2000)
{
Destroy(OBJ001);
OBJ001 = Instantiate(Turrets[1], BuildSpots[0].position, Quaternion.identity)as GameObject;
currentTech = Tech.Level2;
}
if(currentTech == Tech.Level2 && G$$anonymous$$.enemy_exp>= 3200)
{
Destroy(OBJ001);
OBJ001 = Instantiate(Turrets[2], BuildSpots[0].position, Quaternion.identity)as GameObject;
currentTech = Tech.Level3;
}
I just need to do this alot of times, Also i have 4 BuildSpots total. Shall i use 4 funktions to handle every spot seperatly?
Answer by Ibzy · Jun 24, 2015 at 07:49 AM
Is the above code running in the Update() function? If so, why not put it in the same function that adds the exp (or call it from that function)? This way it will only check for >2000 when it has added to the variable.
Currently, yes. Basicly i need to check the exp for the whole time, or evertime it changes. Those change are at least every 2 seconds.
It changes every 2 seconds on a constant ticker, or about every 2 seconds based on an event (e.g. enemy kill)?
Think I can see what's happening. once enemy_exp>=2000 you run through the process...and the enemy_exp is not reset, so on every frame you are destroying and instantiating.
Either adjust your enemy_exp so that it starts from zero after an "upgrade" or flag that the upgrade as already happened and check for that flag?
That's the problem, it will get destroyed and instantiate once per frame, thats the problem. resetting exp is no option. Checking and flagging that over and over again seems not very comfortable, Since i have 3 upgrade possibilitys per 6 Tiers, per 4 Buildspots.
Your answer
Follow this Question
Related Questions
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
Problem with timed code-execution (audioplay and object destruction) 1 Answer
Trying to make a simple inventory: 0 Answers
Destruct gameobject by call from another script 1 Answer
GameObject.FindGameObjectsWithTag still finding destroyed object (C#) 1 Answer