Search Unity

Setting the mass on a Rigidbody2D via script - broken?

Discussion in '2D' started by mrsquare, Nov 15, 2013.

  1. mrsquare

    mrsquare

    Joined:
    Oct 25, 2013
    Posts:
    281
    Hey folks - I've asked this question here too, but just in case that gets ignored, I thought I'd post it here as well.

    Basically, I need to create some 2D physics objects dynamically, so I'm trying to set the mass of my Rigidbody2Ds via script. Neither of these two approaches work:

    Code (csharp):
    1.  
    2. [RequireComponent(typeof(Rigidbody2D))]
    3. public class TestRigidBody : MonoBehaviour
    4. {
    5.     void Start()
    6.     {
    7.        GetComponent<Rigidbody2D>().mass = 0.1f;
    8.     }
    9. }
    10.  
    Code (csharp):
    1.  
    2. public class TestRigidBody : MonoBehaviour
    3. {
    4.     void Start()
    5.     {
    6.        Rigidbody2D newRigidBody = this.gameObject.AddComponent<Rigidbody2D>();
    7.        newRigidBody.mass = 0.1f;
    8.     }
    9. }
    10.  
    I can set the gravityScale, drag amounts etc. fine, but the mass always remains at 1. Altering the mass manually via the inspector works as you'd expect.

    What gives? I've boiled it down to literally the simplest possible scenario, so unless I'm doing something incredibly stupid, it seems a bit broken... :(
     
    Last edited: Nov 15, 2013
  2. RavenBlackX

    RavenBlackX

    Joined:
    Nov 17, 2013
    Posts:
    3
    I don't think you're missing something, because I have just encountered the exact same thing. I also tried setting it during the Start function, in case it was getting reset some time after creation but before that, but it still doesn't work, as doesn't setting it during "Update". So I think it's a bug.
     
  3. Pix10

    Pix10

    Joined:
    Jul 21, 2012
    Posts:
    850
    Yep, doesn't work - bug report it.
     
  4. mrsquare

    mrsquare

    Joined:
    Oct 25, 2013
    Posts:
    281
    Cheers guys - thought so, I'll stick in a bug :)
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,474
    I can confirm that this is a bug and has been fixed. It will be in the next release.