unity 3d student

Beginner B15 – Adding Components via Script

How to add a component to an object using scripting, and ensure it isn’t added more than once.

Code Used (Javascript)

function OnCollisionEnter (myCollision : Collision) {
  1.  if(myCollision.gameObject.name == "Platform"){
  2.   if(!myCollision.gameObject.rigidbody){
  3.    myCollision.gameObject.AddComponent(Rigidbody);
  4.   }
  5.  }
  6. }

Further Reading

Share via Social Media

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

35 Comments