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

39 Comments