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

SendMessage not working properly

Discussion in 'Scripting' started by DroidifyDevs, Sep 23, 2016.

  1. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    Hello,

    So I want to send a message to another gameobject. Here's what I'm doing:

    Code (CSharp):
    1. OtherRowControllerGameObject.GetComponent<RowController>().SendMessage("TESTMoveOtherRows");
    And on the other RowController I'm doing:

    Code (CSharp):
    1. public void TESTMoveOtherRows()
    2.     {
    3.         Debug.Log("Testing!");
    4.     }
    But when I run I get:
    SendMessage TESTMoveOtherRows has no receiver!
    UnityEngine.Component:SendMessage(String)
    RowController:DieFunction() (at Assets/Scripts/RowController.cs:50)
    <CheckIfDead>c__Iterator1:MoveNext() (at Assets/Scripts/RowController.cs:66)
    UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)

    What am I doing wrong so it isn't receiving the message?

    Sorry for the noob question, it's the first time I'm using SendMessage.
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,376
    if you're grabbing a reference to the component that has the method... why not just call the method?

    Code (csharp):
    1.  
    2. OtherRowControllerGameObject.GetComponent<RowController>().TESTMoveOtherRows()
    3.  
     
    DroidifyDevs and JoeStrout like this.
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    I believe you need to call SendMessage on a GameObject, not on a specific component.

    But I'm not really sure... I hardly ever use it, because it's an abomination.
     
  4. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    Yup, that's exactly what I had to do. Also a bug in my brain made me call RowController when I needed AllRowController, which made me think there was a problem with SendMessage.

    Yeah I don't really see a reason to use SendMessage, I just wanted to try it out but so far it seems pointless.
     
  5. NickAtUnity

    NickAtUnity

    Unity Technologies

    Joined:
    Sep 13, 2016
    Posts:
    84
    If you know the exact type of component you want to talk to and can make the method public, that's clearly better than SendMessage. But if you're making a system that you want to work with multiple decoupled types, SendMessage can be helpful. For example, in a first person game you could have your player character use SendMessage to send "OnPlayerInteracted" to objects when the player clicks the mouse. Then you can have any number of component types that implement that method in different ways without the player script having to know about all of them.

    Naturally there are other mechanisms and architectures that can implement similar functionality, but SendMessage is available as a simple way to handle this type of decoupled communication.

    Also, when using SendMessage, you don't have to bother calling GetComponent; you can just call it on the GameObject itself which will invoke it on all the components which is how you keep the scripts from being coupled together.
     
    DroidifyDevs likes this.