- Home /
How to determine which GameObject to destroy?
I have this script here that will award gold to the player if the game object(AIPlayer) "dies" (if his health
void Awake ()
{
currency = GameObject.FindGameObjectWithTag("GameManager").GetComponent<Currency>();
player = GameObject.FindGameObjectWithTag("AIPlayer").GetComponent<AIPlayer>();
}
void Update ()
{
if(player.HP <= 0)
{
Destroy(this.gameObject);
currency.CalculateGold(25);
}
else if(player.HP > 0)
{
}
It is not clear what are you doing. Is that script attached to your player? or to each enemy? If it is attached to each enemy, aside from the AIPlayer script, then you can try:
player = GetComponent<AIPlayer>();//Find the AIPlayer attached to this same GameObject
//If you use FindGameObjectWithTag("AIPlayer") it will search all the scene and return only one object, but you said you have several enemies
Yes this script along with the AIPlayer script are attached to the enemy. Four enemies are instantiated at runtime from a prefab that is Tagged with AIPlayer. So when it says to Destroy this.gameobject it destroys all clones of the prefab in the scene with that tag? I am sorry I cannot explain better. Here is a quick video of the problem on dropbox. https://www.dropbox.com/s/f41jq325wmo6fu9/bandicam%202015-01-21%2003-23-57-255.avi?dl=0You will see once the player takes his turns and then the AI takes their turns that upon killing the instantiated AI Player, all instantiated AI Players are destroyed and the player is awarded gold for all 4 kills even though only one was actually killed, they were all destroyed.
Answer by Superrodan · Jan 21, 2015 at 09:40 AM
Does it work if you change this line:
player = GameObject.FindGameObjectWithTag("AIPlayer").GetComponent<AIPlayer>();
to
player = this.gameObject.GetComponent<AIPlayer>();
Because this script is on every enemy, I believe it should be able to find the component of itself rather than finding the component using the tags.
Wow. That does fix it. Idk why I couldn't see that, guess it just needed fresh eyes. Thanks alot.
Also, sometimes the problem is something different than it seems. I would double check to be sure that whatever is causing the enemies to take damage is only affecting one of the enemies at once. It could be that all of the enemies are taking damage when one gets hit.
If that were happening then they would all reach 0 HP at the same time and die.
Yeah I did already check to see if they were taking damage at the same time, and they were not. It seems that it was just because they shared a tag.
Answer by AndresBarrera · Jan 21, 2015 at 09:45 AM
The problem is that FindGameObjectWithTag will look for an object in all your scene, so your enemies may end referencing other enemies through the 'player' variable. Since both scripts (AIPlayer and this one) are attached to the same GameObject, you only need to use a GetComponent to find the specific AIPlayer for the object.
player = GetComponent<AIPlayer>();
Your answer
Follow this Question
Related Questions
How to destroy bullets? 2 Answers
How do I put an image inside text? 2 Answers
Script won't destroy prefab clones when overlapping... plz help 1 Answer