Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to use SendMessage?

Discussion in 'Scripting' started by DaleNation, Jul 10, 2014.

  1. DaleNation

    DaleNation

    Joined:
    Jul 6, 2014
    Posts:
    30
    Hello,

    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:
    Code (CSharp):
    1.     // Update is called once per frame
    2.     void Update () {
    3.  
    4.                
    5.                 Vector3 fwd = transform.TransformDirection(Vector3.forward);
    6.                
    7.                 RaycastHit hit;
    8.                
    9.                 playerpos = transform.position;
    10.                
    11.                 if(Physics.Raycast(playerpos, fwd, out hit,100f)){
    12.                     this.gameObject.SendMessage("receive",hit.transform);      
    13.                 }
    14.         }
    15.  
    16.    
    and the script on the cube:

    Code (CSharp):
    1.         void receive(Transform coll){
    2.             if(coll == this.transform){
    3.                 DisplayThis = true;
    4.             }else{
    5.                 DisplayThis = false;
    6.             }
    7.         }
    but when i hit the cube with my raycast, I get the error:

    What does that mean that it has no receiver?
     
  2. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    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.
     
  3. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
    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:

    Code (c#):
    1.  
    2. hit.gameObject.SendMessage("recieve", hit.transform);
    3.  
     
  4. DaleNation

    DaleNation

    Joined:
    Jul 6, 2014
    Posts:
    30
    I get this error:

    I'm not familiar with raycasting at all, so I'm not really sure why a hit doesn't have a gameObject
     
  5. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,491
    I second this, it's performance is terrible and it is incredibly hard to debug. I would avoid it at all costs.

    As for the error mesage, novashot explained it nicely.
     
  6. DaleNation

    DaleNation

    Joined:
    Jul 6, 2014
    Posts:
    30
    I'm afraid that for my level of knowledge of programming, SendMessage will have to do.

    And after using Novashot's code I got that new error message.
     
  7. DaleNation

    DaleNation

    Joined:
    Jul 6, 2014
    Posts:
    30
    Ah, I had to use hit.transform.gameObject.SendMessage("receive", hit.transform);

    thank you all
     
  8. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,491
    Try this:

    Code (csharp):
    1.  
    2. class SomeClass : MonoBehaviour
    3. {
    4.     public void receive()
    5.     {
    6.         DisplayThis = true;
    7.     }
    8. }
    9.  
    10. class RayCaster : MonoBehaviour
    11. {
    12.     void Update()
    13.     {
    14.         Vector3 fwd = transform.TransformDirection(Vector3.forward);
    15.         RaycastHit hit;
    16.         playerpos = transform.position;
    17.  
    18.         if (Physics.Raycast(playerpos, fwd, out hit, 100f))
    19.         {
    20.             hit.transform.GetComponent<SomeClass>().receive();
    21.         }
    22.     }
    23. }
    24.  
     
    rafftune likes this.