- Home /
Ai should delete a build turret
The AI builds turrets at a given position when he/she has enouph exp.
if(GM.enemy_exp >= 600)
{
if(BuildSpots[0].tag == "TurretSpotOpen")
{
Instantiate(Turrets[0], BuildSpots[0].position, Quaternion.identity);
BuildSpots[0].tag = "TurretSpotClosed";
}
}
Now at another state i want the AI to delete that turret, to make space for a new one how could i achieve this? I mean, how do i get the right object to destroy? How can i store the already build object to get rid of it later?
I bet it's super simple, but sometimes i'm blind.
Thanks in advance
Answer by Cherno · Jun 23, 2015 at 11:39 PM
You could have a List of the turrets that were built, like this:
using System.Collection.Generic;
private List<GameObject> builtTurrets = new List<GameObject>();
You can make the Instantiate function return the gameobject that it created, so you can add this GO into the turret list.
gameObject newturret = Instantiate(Turrets[0], BuildSpots[0].position, Quaternion.identity) as GameObject;
builtTurrets.Add(newturret);
To destroy the turrets, you just access the references to them stored in the list. How you differentiate between turrets is up to you; Give them a unique ID in one of their scripts, change their name and add a number to it, etc. You can then, if you don't know the turret's index in the list, iterate through the list and check if the currently iterated turret's ID or whatever corresponds to the one you are searching for, and then destroy that turret (don't forget to remove it from the list first!).
Thanks you showed me the way i wanted it to be, kind of.
That's the result i wanted to achieve.
if(G$$anonymous$$.enemy_exp >= 700)
{
if(BuildSpots[0].tag == "TurretSpotClosed")
{
Destroy(OBJ001);
BuildSpots[0].tag = "TurretSpotOpen";
}
}
else if(G$$anonymous$$.enemy_exp >= 600)
{
if(BuildSpots[0].tag == "TurretSpotOpen")
{
OBJ001 = Instantiate(Turrets[0], BuildSpots[0].position, Quaternion.identity)as GameObject;
BuildSpots[0].tag = "TurretSpotClosed";
}
}
I only have 4 buildspots and not all are the whole time active, so this works for all 4 later on, i hope. :P
In that case, a generic array might be better, because it will stay at the same size wehter it has elements in it or not. So you could have an array with a Length of 4, and each position inside the array corresponds to one of the four buildings spots. That'll make it easy to directly access one of them, because you probably will know the number of the spot.
Yep i will know the number, since i need to plan the AI action ahead.
The wierd thing when i do it like that the 2nd gun is not acting. Any ideas why?
when i place a 2nd gun on the spot i mean.
Your answer
Follow this Question
Related Questions
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
Store a Copy then Destroy Scene Objects 1 Answer
Deleting instantiated prefabs clones? 2 Answers
Problem with timed code-execution (audioplay and object destruction) 1 Answer
Trying to make a simple inventory: 0 Answers