Search Unity

SendMessage with receiver in c#

Discussion in 'Scripting' started by jorsalsa, Aug 28, 2010.

  1. jorsalsa

    jorsalsa

    Joined:
    Aug 25, 2010
    Posts:
    8
    How can I program SendMessage() with a receiver in C#?
    In JS I can use in one object SendMessage like("nameOfReceiverfunction", true);
    and in the receiving object:
    function nameOfReceiverfunction (on: bool)
    {
    //do what you need with this message
    }
    How do I achieve the same in C#? (I know I can use GetComponent(), but I would like to really use SendMessage. Do I need delegates? .NET event handling?

    Thanks for your answer.
     
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    It looks exactly the same as in js, difference is that the function declaration looks slightly different as you can see below.
    But the rest is 100% equal and a single sendmessage can trigger a unityscript, C and boo function in the same go on the same GO if it wants to
    Code (csharp):
    1. void nameOfReceiverfunction (bool on)
    2. {
    3. //do what you need with this message
    4. }