sending and recieving messages in scripts?

Hi I am new to c# and I’ve been having trouble knowing how to send a message from one script and receiving it in another script I am making. My main hope was to have a raycast that sends a message to the object it hit, so that i could make a raycast gun in a sense. I can use raycasts fine and everything else i can do just any help on the message sending or receiving would be great or even another way of doing it.

Depends on how you’re wanting to handle the messages honestly.

One way would be switch/case handling in a script. For example, you have a class called Player, attached it to an object. On raycast you hit the object, check if it has the Player component, and call the message method.

object.GetComponent().message(“Hit”);

In the Player class, under the message method… you would have something as the following…

void message(string action){
case "Hit":
    //Do hit action;
case "Miss":
    //Do miss action
case "Open":
    //Do open action
}

This is a raw code example for you to trigger events by strings or messages. I’m not sure how else to answer your message, without going into your own situational logic in your code as in if the shooter of the raycast is an attacker, and your target is a living entity, then call the player.attack() as opposed to player.message(“Attack”). What you need to figure out is if you want to go with the switch/case or the method based(more optimised/better coding result).