This is a general question. I have an if statement inside a Physics.Raycast that detects if a “laser switch” has been hit and if so if call for a function.
Method 1:
if (hit.collider.tag == "LaserSwitch")
{
hit.transform.GetComponent<LaserSwitch>().LaserRaycastHit();
}
Method 2:
if (hit.collider.TryGetComponent<LaserSwitch>(out var @switch))
{
@switch.LaserRaycastHit();
}
Since I can’t cache “hit.transform.GetComponent()” since the laser switch may be different every time which method is less expensive or is there a better way to do this?