- Home /
Get TYPE script from raycast target
Is there a way to get a script from a raycast target but it’s not a certain script. For example, there’s a cat, dog, and Panda and I shoot one of them, then is there a way to get the script from the animal I hit no matter what script it is so if I have a panda script for the panda and a cat script for a cat, is there a way to get the script component from raycast even though they have different scripts.
Sorry, I know this is confusing but I hope you know what I mean
Thanks for your help
Answer by AaronXRDev · Nov 06, 2018 at 02:08 AM
I would actually go the other route, create a specific handler in each of the objects that could possibly be targeted and call it.
Something like this:
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.forward, out hit, 10f))
{
hit.collider.gameObject.SendMessage("ISeeYou");
}
Then in each of the scripts that could possibly be hit by this, add a method:
public void TagYouAreIt()
{
Debug.Log(transform.gameObject.name);
//Handle specific actions for this class here
}
Thanks, I didn’t even know there that the sendmssage function existed. I researched it a bit and it helped a lot. Thanks for your help.
Your answer
![](https://koobas.hobune.stream/wayback/20220612181615im_/https://answers.unity.com/themes/thub/images/avi.jpg)