I have a game Object that is an AI. It attacks all enemies with the tag "Enemy". But If his own tag is also "Enemy", how can I make him ignore it, while still keeping the tag "Enemy"?
There has to be a term or function for ignoring self tag. Oh maybe thats it! How to ignore self tag??
Answer by Commoble
Mar 08 at 05:32 AM
Once you have the gameobject and you've verified that it has the correct tag, check whether it's the same gameobject as the object doing the checking:
if (gameObjectWithTag != this.gameObject)
ok thats great. but what if I want the gameobject to attack, even if its own tag is "Enemy"? I don't want him to do nothing if everybody has the tag "Enemy"
Doesn't matter, the code I gave you is still relevant -- the bit I gave you just checks whether another gameobject is the same object as the object doing the checking, and executes the code inside the if block if it isn't, it doesn't look at the tags at all.
I'll try to make it clearer by showing it alongside checking the tag:
// let otherObject be a GameObject variable from somewhere
if (otherObject.compareTag("Enemy") && otherObject != this.gameObject)
// attack code
thanks! man haha
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
118 People are following this question.
How get position from a game object?
how do i delete a gameObject if touches another of its kind?
How to check if GameObject exists in hierarchy (is instantiated)
Game working perfect in editor but not working properly in mobile
Create an easy craft system