- Home /
See edit
Comparing tag not working
I have a lot of experience with Unity and C# and been working with it for over 2 years, yet even this has me stumped. I'm trying to call a function if the player touches an object with tag "Model". I've checked, and the object the player taps on is the one I want AND it has that tag, but it just won't enter the if statement. I tried using my touchedObject variable and getting the hit object directly to no avail. PLEASE HELP!
Ray ray = Camera.main.ScreenPointToRay(touch.position);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100))
{
touchPosition = hit.point;
touchedObject = hit.transform.gameObject;
debugText.text = touchedObject.name; //THIS WORKS AND DISPLAYS THE CORRECT OBJECT
//NEVER ENTERS THIS IF STATEMENT
if (touchedObject.CompareTag("Model")
|| hit.transform.gameObject.CompareTag("Model"))
{
debugText.text = "Model touched";
InteractWithNurchum();
}
}
else
{
touchPosition = Vector3.zero;
touchedObject = null;
}
Edit: the debugText.text is just a UI version of Debug.Log()
EDIT #2: I've been a complete idiot. I have another debug in the function called in the if statement which prints the name. My code is working fine.
You are displaying the name of the gameObject but you are comparing the tag
I'm aware of that. I only display the name to check that the raycast is hitting the gameObject (essentially Debug.Log()) I want. That gameObject has the "$$anonymous$$odel" tag
I guess it hasn't otherwise, you would enter the if
statement... $$anonymous$$ake sure there isn't any invisible character (in your code AND in the Tag $$anonymous$$anager). And display the tag of the hit object :
debugText.text = string.Format( "{0} ({1})", touchedObject.name, touchedObject.tag );
Follow this Question
Related Questions
[C#] Raycasts and Object Tags 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Clicking on an Object to Make it the Variable Target 1 Answer
Player Attack Script. I Need Help! 0 Answers