- Home /
C# How to Check If a Gameobject's tag doesn't equal a Specific Tag
Hi everyone, I'm trying to make an if statement that checks if a gameobject's doesn't equal a specific tag.But the if statement isn't working and the code is executed.Any idea what is wrong?
public bool somebool = false;
void Update(){
RaycastHit hit;
if(Physics.SphereCast(gameobject.transform.position, 1f, gameObject.transform.forward, out hit, 50)){
if(gameObject.tag != "SomeTag"){
somebool = true;
}
}
}
Any solution? I have the same problem. my sphere cast is detecting that I am grounded when I hold my VR weapons under my Rididbody Playercontroller
Answer by TheDarkVoid · May 14, 2013 at 11:09 PM
I think what you want is this:
public bool somebool = false;
void Update(){
RaycastHit hit;
if(Physics.SphereCast(gameobject.transform.position, 1f, gameObject.transform.forward, out hit, 50)){
if(hit.collider.tag != "SomeTag"){
somebool = true;
}
}
}
Answer by Lairinus · May 14, 2013 at 11:14 PM
You should try using gameObject.CompareTag().
public bool somebool = false;
void Update(){
RaycastHit hit;
if(Physics.SphereCast(gameobject.transform.position, 1f, gameObject.transform.forward, out hit, 50)){
if(!gameobject.CompareTag("SomeTag")){
somebool = true;
}
}
}
I hear it's faster than the other way as well.
Answer by sebasvanhalewijn · Feb 18, 2020 at 04:53 PM
nevermind i'm a dumbo i didn't assign the tag lmao
Your answer
Follow this Question
Related Questions
Saving final score and displaying on main menu 1 Answer
null texture passed to GUI.DrawTexture 0 Answers
How to Name Individual Buttons Within a List 2 Answers
c# Adjust In-Game audio 1 Answer
C# How to Detect Edges of a Collider 0 Answers