Search Unity

How to simply print a variable on screen?

Discussion in 'Scripting' started by DanjelRicci, Mar 8, 2010.

  1. DanjelRicci

    DanjelRicci

    Joined:
    Mar 8, 2010
    Posts:
    310
    Hi everyone!
    This is my first post here, but I already started to use Unity some months ago, and posted something in UnityAnswers (the F-Zero like magnetic vehicle issue).

    Currently I'm working on a simple kart game, something like Mario Kart, and topics in this forum are really helping me. :D

    Now, my problem of the day:
    I need to print the "rigidbody.velocity.magnitude" variable on the screen, in order to know its amount and then know how to modify other values on the base of that. I think the better solution is using the GUI sistem, but how can I do it?
    Thank you! :)
     
  2. Mirage

    Mirage

    Joined:
    Jan 13, 2010
    Posts:
    230
    Code (csharp):
    1. function OnGUI()
    2. {
    3.   GUI.Label(Rect(0,0,100,100),variableName)
    4. }
    That should print the variable "variableName" in the upper left corner of the screen.[/code]
     
  3. RobbieDingo

    RobbieDingo

    Joined:
    Jun 2, 2008
    Posts:
    484
    .. or, I tend to use this a lot to debug stuff:-

    print("The variable is :"+variableName);

    ...this prints the variable's value to the bottom of the editor window, and to the console/log, not to screen, but is very handy.
     
  4. Sycle

    Sycle

    Joined:
    Nov 24, 2009
    Posts:
    446
    Mirage's solution is correct, although there's a slight gotcha in that GUI.label expects a string, so if you're trying to display a float you'll need to do something like this :

    Code (csharp):
    1. function OnGUI()
    2. {
    3.   GUI.Label(Rect(0,0,100,100), rigidbody.velocity.magnitude.ToString());
    4. }
     
    seansteezy likes this.
  5. DanjelRicci

    DanjelRicci

    Joined:
    Mar 8, 2010
    Posts:
    310
    That's perfect, thanks everyone!

    I found the RobbieDingo's solution more handy: I completely forgot that I could print variables directly on the debug menu!

    Well, moving on, now adjusting my bad kart settings...
     
    seansteezy likes this.
  6. R0b0tKyl3

    R0b0tKyl3

    Joined:
    Jan 18, 2018
    Posts:
    1
    Mirage's solution almost worked for me, but i keep getting an error where it says GUI.Label(Rect(
    with the problem being that i can't use Rect right there. anyone know how to fix that?
     
  7. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,536
  8. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    If you just want to see what a private variable is doing, you can just set the inspector to debug mode btw. If it's a variable decalred inside a method, then debog.log will do.
     
  9. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Mind posting the actual error message, not your interpretation of it?
     
  10. ecpeter23

    ecpeter23

    Joined:
    Oct 24, 2019
    Posts:
    1
    idc that this is a decade old thread but I keep getting the error message "Non-invocable member: "Rect" cannot be used like a method." any help.
     
  11. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    The code posted was in Unity Script. In C# you need to put "new" in front of rect.
     
  12. djjag

    djjag

    Joined:
    Mar 16, 2013
    Posts:
    1
    Error CS1955 The uncalled member "Rect" cannot be used as a method.
     
  13. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
  14. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Technically this isn't irrelevant, if you try to use the UnityScript code posted in 2010 into a C# script, that is the error you'd get, so I assume that's what djjag did (after finding this thread by googling for something similar.)

    Of course, had djjag bothered to actually read the thread, they'd have seen the solution to their error in the comment immediately before theirs.
     
    Kurt-Dekker and PraetorBlue like this.
  15. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    You got me there... :)

    I wish Unity would lock threads at the one-month mark, or after no activity for a month. It's so easy to create a fresh post and then just link the thread that Google finds.

    I wish there was also a way to permanently prevent UnityScript results ever being returned by Google, unless you actually include the term "javascript" or "unityscript" in your search because you're supporting old software...

    If wishes were fishes...

    OP: look up any new tutorial on the Unity UI system. Software moves a lot in 10 years.
     
    StarManta likes this.
  16. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    A month might be a little bit too quick for an auto-lock, but yeah I definitely wish there was an auto-lock of some kind in place.