- Home /
Question by
LeftyTwoGuns · Aug 06, 2014 at 10:31 PM ·
raycasthitsendmessageextend
How to send a raycastHit to another script?
Say I'm wanting to do something to an object that is hit by a raycast. But the raycasting and what happens when hit are done in two separate scripts. How would I send that raycast hit-point to the script that needs it?
I know there's SendMessage, but wouldn't that be a big performance hit to send a message every time there is a raycast hit? Would extending one of the scripts work?
Comment
Answer by AyAMrau · Aug 07, 2014 at 12:04 AM
Assuming the script you want to access is on the object hit by the raycast this is how you can access it:
...
RaycastHit hitInfo;
// use the version of raycast you need, but it needs to include the hitInfo
if (Physics.Raycast(ray, out hitInfo))
{
ScriptName otherScript = hitInfo.collider.gameObject.GetComponent<ScriptName>();
if(otherScript != null)
{
otherScript.Method();
}
}