I have 2 scripts. One is attached to a cube, and the other to my player. I’m raycasting from the player and sending the info to the script on the cube.
Player code:
// Update is called once per frame
void Update () {
Vector3 fwd = transform.TransformDirection(Vector3.forward);
RaycastHit hit;
playerpos = transform.position;
if(Physics.Raycast(playerpos, fwd, out hit,100f)){
this.gameObject.SendMessage("receive",hit.transform);
}
}
How to use SendMessage? Don’t use SendMessage. It’s a very bad form of code and just about against every good design pattern.
If you know the type of script on the Cube, do GetComponent. If you don’t know the type and many can have the “receive” method, implement an interface and do GetComponent of that interface.
it means your object you are sending the message to doesn’t have the function you are trying to call. your send message statement is wrong… you are sending the message to yourself… you want: