Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

How do I add a rigidbody to a gameobject in Unity? (JavaScript)

Discussion in 'Scripting' started by Kman43759, Nov 16, 2014.

  1. Kman43759

    Kman43759

    Joined:
    Nov 16, 2014
    Posts:
    5
    Alright so first of all im new to Unity, and new to Unity Javascript and im an amateur at Programming (But im not new). So anyway I made a sphere and then I created a new script for the sphere that was supposed to add a rigidbody to the sphere and then it was supposed to make the sphere move in the x axis for a certain distance everytime function Update was called and then subtract .1 from the velocity... so I finished writing it, I saved it, made sure the script was attached to the sphere and then tested the game............. the sphere would not move and I got this error:
    MissingComponentException: There is no 'Rigidbody' attached to the "Sphere" game object, but a script is trying to access it.
    You probably need to add a Rigidbody to the game object "Sphere". Or your script needs to check if the component is attached before using it.

    MY CODE:
    #pragma strict

    function Start () {
    rigidbody.velocity.x = 15;
    var rigbody : Rigidbody;
    rigbody = gameObject.AddComponent(Rigidbody);
    }

    function Update () {
    if(rigidbody.velocity.x >0) {
    rigidbody.velocity.x -= 0.1;
    }else{
    rigidbody.velocity.x = 0;
    }
    }
     
  2. Kman43759

    Kman43759

    Joined:
    Nov 16, 2014
    Posts:
    5
    Ok so I fixed it... You have to click on your gameobject, add component, press physics, press rigidbody ;) Lol hopefully this helped somebody who had the same problem as me
     
    sabi22 and VbManSd like this.
  3. heizenhamza

    heizenhamza

    Joined:
    Aug 17, 2018
    Posts:
    1
    After 4 years there is someone how want to say thank you so much lol hahahaha (i have the same error)
     
  4. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    26,729
unityunity