I have a function that does a Raycast
and when it hits the correct object, it should do something specific with that object. The problem is that I cannot figure out how to get the object that extends from MonoBehavior
.
Here is the code I’ve been using:
void checkForHit(Vector3 position) {
var ray = Camera.main.ScreenPointToRay(position);
RaycastHit rayHit;
if(Physics.Raycast(ray, out rayHit)) {
if(rayHit.collider.tag == "specificTagToThisObj") {
var monoObj = rayHit.transform.gameObject as MyMonoBehaviour; //This line has an error.
}
}
}
Is this possible to do?