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

21 Comments

    Hi, I have a problem here. I tried attaching the addComp script to a gameObject in the scene and it works fine when colliding. However when I tried to attach the addComp script to a prefab, ie. bullet, it wont work. Have I done something wrong? Please advise. Thanks.

    • Hi Jay, post your script via pasteit4me.com and post a link here so we can all take a look at it please!

    Hi wgstone,
    Still no luck…:(

    I changed the code to:

    function OnCollisionEnter (myCollisions : Collision) {

    if(myCollisions.gameObject.name == “Cube1″){
    var myCounter : GUITextScript = gameObject.Find(“GUITextScript”).GetComponent(“Counter”);
    myCounter.Counter+1;
    }
    }
    I get a: “BCE0034: Expressions in statements must only be executed for their side-effects.”
    I googled the error and it means: “The compiler assumes that statements which contain nothing but values and which don’t have any side effects (no function calls or assignment) are a mistake and emits an error accordingly.”

    Still, I have NO IDEA what to do…Help please??

  • Sorry, wrong page…reposting it to the correct one…(I need rest…)

  • Excellent code, very clever. The if statement is because it collides several times?

    • Precisely, the script then only attempts to add a rigidbody if it does not find a rigidbody.

    var TriggerSign : gameObject;
    var signWarning : gameObject;

    function OnCollisionEnter( myCollision : Collision ){
    if(myCollision.gameObject.name == “TriggerSign”){
    Instantiate (signWarning);
    }

    triggersign is e name of my cube(gameobject)
    signWarning is e name of my sign(gameObject)
    car is my player(gameObject)

    what i want to do is on the collision of my car with the cube , signWarning(sign) will appear but the codes doesn’t seems to work . what went wrong . hopefully u guys can help thanks you

    • Don – you haven’t written the Instantiate command properly, take a look at the video here on how to do that, if you’ve reached B15 then you should have already encountered it. Also if your triggersign object has a collider set to trigger, then you should use OnTriggerEnter, not OnCollisionEnter.

    if i use OnTriggerEnter then how do i code it? really thanks a ton for your help

  • is this the right way?

    var signWarning : GameObject;

    function OnTriggerEnter (myTrigger : Collider) {
    if(myTrigger.gameObject.name == “TriggerSign”){
    var instance : GameObject = Instantiate(signWarning, transform.position, transform.rotation);
    }
    }

    • looks okay to me don, whats the context?

    hmmm the problem’s solved already thank alot… anw i jus wanna ask if it is possible for ontriggerenter event to work… when ontrigger i start my timer?becus i tried doing that but the timer only records the time my car collides with my cube and exiting it..

  • Hi Don, yes you’re not considering when the functions occur – you need to update time based stuff in an Update() or FixedUpdate() function, so to get a timer to start when the trigger collision occurs, simply make a boolean flag variable to set to true when you collide, and only add to the timer when this variable becomes true. Does that make sense?

  • ok i almost get what u’re trying to say… so my update function will have to be under the same script or do i hav to create another script den when my car collides and triggers e cube i add in a line and call my function in my script?

  • What if you wanted to do the opposite? and remove a component such as a box collider?

    • To remove a component use GetComponent within a Destroy command, eg. Destroy(gameObject.GetComponent(Rigidbody)); for example, good luck!

    hi. i have a problem.

    i want to add a mesh collider to a dynamic gameobject.

    var mygameobject : GameObject;
    mygameobject = new GameObject();

    mygameobject = Instantiate(aPrefab, jugador.transform.position, jugador.transform.rotation);

    mygameobject.AddComponent(MeshCollider);

    mygameobject.GetComponent( Transform).localScale = new Vector3(3.5, 3, 3.5);

    But it didn’t work.

    help please.

    Take care.

  • hi. i have a problem.

    i want to add a mesh collider to a dynamic gameobject.

    var mygameobject : GameObject;
    mygameobject = new GameObject();

    mygameobject = Instantiate(aPrefab, jugador.transform.position, jugador.transform.rotation);

    mygameobject.AddComponent(MeshCollider);

    mygameobject.GetComponent( Transform).localScale = new Vector3(3.5, 3, 3.5);

    But it didn’t work.

    help please.

    Take care.

    • What does happen here? Always mention what occurs when asking otherwise it’s not always a singular finite answer.

    In C#, it has to be “Rigidbody” withing quotes. Otherwise, it says that AddComponent requires a value not a type.

  • I did a really cool domino effect using multiple domino pieces which I called “dom”. I applied this script only to the first object.


    function OnCollisionEnter (domCollision : Collision) {

    if(domCollision.gameObject.name == "dom"){
    if(!domCollision.gameObject.rigidbody){
    domCollision.gameObject.AddComponent(Rigidbody);
    }
    if(!domCollision.gameObject.DominoAdd){
    domCollision.gameObject.AddComponent(DominoAdd);
    }
    }
    }

Leave a Comment