Search Unity

Question on retrieving mass from an object.

Discussion in 'Scripting' started by jneumann2, Apr 6, 2011.

  1. jneumann2

    jneumann2

    Joined:
    Apr 6, 2011
    Posts:
    6
    Hey guys, this is my first post here on the forums.

    I'm currently working on a game that involves momentum, which is mass x velocity,

    I need to display the mass, velocity, and momentum of your character object. How would I retrieve the mass/velocity from the objects script and display it on the GUI?

    Thanks,
     
  2. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Mass and velocity are both public properties of the 'rigid body' class, and can be read directly.

    For displaying the values on screen, GUI/GUILayout.Label() would probably be the easiest solution.
     
  3. jneumann2

    jneumann2

    Joined:
    Apr 6, 2011
    Posts:
    6
    Thanks for responding Jesse,

    I'm able to display Text on the screen now. But I'm still unsure on how to make the value appear. I'm used to java, and when using java and you want to display, say the mass of an object, I would write:

    System.out.println("MASS: " + mass); //Mass is a variable.

    So I have:

    GUI.Label (Rect (9, 400, 100, 20), "MASS: ");

    but I can't do,

    GUI.Label (Rect (9, 400, 100, 20), "MASS: " + mass);

    So, how would I display the certain mass of a certain object in the gameworld?
     
  4. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Try:

    Code (csharp):
    1. GUI.Label (Rect (9, 400, 100, 20), "MASS: " + mass.ToString());
     
  5. jneumann2

    jneumann2

    Joined:
    Apr 6, 2011
    Posts:
    6
    Thanks Jesse!

    I used: rigidbody.mass and it displays.