- Home /
Checking raycast hit on tagged collider- "Cannot implicitly convert type 'string' to 'bool'"
I'm trying to make a locational damage system where the raycast checks the tag of the bone collider and sends the appropriate amount of damage. But apparently I'm not understanding how to check a tagged collider.
Ray ray = new Ray(m_Transform.position, transform.forward);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, Range){
if(hit.collider.tag = "EnemyHead"){
Debug.Log("HeadHit!!");
}
}
But that tells me Cannot implicitly convert type 'string' to 'bool'
What am I missing?
Answer by kacyesp · Sep 03, 2014 at 10:25 PM
You were missing an equals sign.
Ray ray = new Ray(m_Transform.position, transform.forward);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, Range){
if(hit.collider.tag == "EnemyHead"){
Debug.Log("HeadHit!!");
}
}
You could also use this which is a bit more readable:
if ( hit.collider.compareTag("EnemyHead") )
Although equivalent, compareTag() removes the ambiguity of whether "==" will compare the refernces pointing to the string or the characters in the strings. In Java ( and I'm sure other languages ), comparing string literals with "==" would return false. It's only fine in C# because the '==' is an overloaded operator.
Damn! Lol but I like the CompareTag better, didn't know about that, so thanks a bunch.