unity 3d student

Beginner B28 – SendMessage() to Call External Functions

How to use SendMessage() to call functions in scripts attached to other objects.

Code Used (Javascript) – Reactor.js

var downTexture : Texture2D;
  1.  
  2. function React () {
  3.  renderer.material.mainTexture = downTexture;
  4.  yield WaitForSeconds(1);
  5.  gameObject.AddComponent(Rigidbody);
  6. }

Code Used (Javascript) – Switcher.js

function OnCollisionEnter(col : Collision) {
  1.  if(col.gameObject.name == "Switch"){
  2.   gameObject.Find("Block").SendMessage("React");
  3.  }
  4. }

Further Reading

Share via Social Media

  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Yahoo! Buzz
  • Twitter
  • Google Bookmarks
  • Add to favorites

20 Comments

    Wow!
    New tutorial!
    Thanks, man.

  • Awesome, new stuff coming as promised !

    Thank you for your hard work and dedication, these videos are really precious!

  • Wow! New video! Thanks, this was so helpful. Please post more videos, i love yours.

  • The goodness just keeps on coming!

  • Might seem trivial but would it not make more sense to put the switcher script onto the switch and tell it to look for a collision with the ball?

  • Great tutorial and it’s exactly what I was looking for.
    Thanks!

  • wow
    thanks, man
    you helped me a lot xD

    I’m Wesllei directly from Brazil

  • Brilliant stuff mate; I’m loving your very helpful and concise tutorials.
    I desperately needed this one in particular as I was using an overly complex system where I held a static array of Strings to pass commands to objects, pushed commands like “Camera=Follow”, and then disassembled the strings in a for loop in the receiver script :D
    This is much neater and much more efficient, so thank you!

  • Wow that was super useful, thank you!!!!!!!!

  • Good and clear tutorial. Explained it rather well and thorough.

  • 감사합니다 도움 많이 받고 있습니다

    • 당신은 환영

    Very helpful tutorial!
    Question:
    Is there a way to use “gameObject.Find” to reference an object created with a prefab?

    Example:
    I have a prefab named “GamePiece-Yellow”. What I want to do is spawn an object from the prefab and then move it in a direction.
    I can spawn the prefab but when I try to call the function in the script that I have applied to prefab “GamePiece-Yellow” I get a NullReferenceException error using: gameObject.Find(“GamePiece-Yellow”).SendMessage(“movePieceUP”);

    Is this because when the object is created from the prefab it is named: “GamePiece-Yellow(Clone)” ?

    Thanks!
    -TEK

    • Hi Tek, yes indeed, if you want to avoid that issue, make sure you name your object when you instantiate it- for example (javascript)

      var thing : GameObject = Instantiate(prefabRef, position, rotation);
      thing.name = “ExactlyWhatYouWantToCallit”;

      • That worked, thank you Will!

    Hi,

    The send message example is AWESOME! But what if I have 10 Objects in the scene named “Block”? it seems to only find the first one for some reason ?

    How would I send the message to all of the other blocks with the same name?

    Or do I have to go through and name them Block, Block1, Block2, Block3 etc.. then update the code like

    gameObject.Find(“Block”).SendMessage(“React”);
    gameObject.Find(“Block1″).SendMessage(“React”);
    gameObject.Find(“Block2″).SendMessage(“React”);
    gameObject.Find(“Block3″).SendMessage(“React”);

    The reason Im having trouble with this is what if I have 25 or so Blocks how would I handle this? to send the message to all of them with the same name? or maybe I did something wrong?

    Cheers,
    Zeek

    Ps I love these tutorials keep them coming :)

    • I found Object.FindObjectsOfType in the scripting reference but I don’t understand it to be able to use with the example above to be able to find all of the Blocks with the name Block.

      • See my reply to your original question.

      Just the wrong approach is all. Things like this, make sure you tag them all the same, then use a for loop to iterate the same command for all of them. Something like (javascript) this -

      var blocks : GameObject[] = GameObject.FindObjectsWithTag(“block”);
      for(var theblock in blocks){
      theblock.SendMessage(“React”);
      }

      Here we find everything tagged ‘block’ and then use that array in a for loop, making a variable called theblock to represent the current entry in the array, then for each time we find an entry in the array, we send it a message called React.

      Good luck!

      • Thanks Will :D

        it works perfectly now :) they all drop its sooo cool I added a bouncy material lol

        Again Thank you Sir ;)

Leave a Comment