how to store GameObject.FindWithTag("Name") and use as a tag?
Hello friends, Sorry for newbie question as I am newbie. I am gonna do this:
void OnTriggerStay2D(Collider2D other)
{
if (GameObject.FindWithTag("Human") )
{
//do something
}
}
but the problem is GameObject.FindWithTag() can`t produce a condition for if. So I need to store it some how. I tested below but as trigger is defined with a collider 2d and I am storing in a gameobject then it can`t work. What is the right data type for that instead GameObject? I tried with string and object too and no one could work. Thanks for any help.
GameObject humanTag;
void Start () {
humanTag = GameObject.FindWithTag("Human");
{
void OnTriggerStay2D(Collider2D other)
{
if (other.tag == humanTag)
{
//do something
}
If it helps the tag I need is in the children of a game object so I can`t directly use the parent tag.
Tags aside, what is your goal here? You want to know if the gameObject that hit the collider has a specific tag?
@Vega4Life , exactly! But the problem is the tag I need is in the child of an game object so if I use other.tag == "Human" as my "if condition" then it can`t work. (collider can`t detect it)
The parent game object has a "Player" tag and the child game object has a "Human" tag. For some reason I should use tag "Human" and I can`t use "Player". any suggest?
Can you just put a collider on the child gameObject as well?
Answer by SpasticDad · Dec 28, 2018 at 04:05 PM
I think you might need something like this:
if (other.gameObject.tag == "Human")
{
}
and just not store the tag as a seperate object
EDIT: Alright so the child of the object has the tag. If you know the position of the child then you can call the following:
other.gameObject.GetChild(/here you put the position/)
so the following should help you:
if (other.gameObject.GetChild(0).tag == "Human")
{
}
@SpasticDad I already tested that and it can`t work. that condition can work only if I am looking for tag in parent. I should go deeper to get the child parent somehow.
I updated the answer so maybe that will help you
Your answer
Follow this Question
Related Questions
Find closest object with a certain tag 2 Answers
Usage of multiple tags or some other workaround? 1 Answer
Multiple tags for one GameObject 3 Answers
Change music when enemy is in scene 0 Answers