Search Unity

Display text

Discussion in 'Scripting' started by Paykoman, Sep 4, 2015.

  1. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Hi guys, i use this line of code to display a message when player enter in trigger wt i would like to know is if its possible to divide the height and width by a float number, cuz i try and i get and error. i need text displayed a bit up or right of the center.

    GUI.Label (new Rect (Screen.width / 2, Screen.height / 2, 1280, 800), "Press E To Interact!");

    Appreciate
     
    Last edited: Sep 4, 2015
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    in almost all cases, if you find yourself writing that sentence you need to include the error (all of it, copy it out of the console)


    The code above will work fine... although it has to be within an OnGUI function in order to work.


    Having said that. Any reason for learning the old system rather than the new system released in 4.6+? Far simpler to learn and use. OnGUI approach is pretty much just for editor scripting these days.

    https://unity3d.com/learn/tutorials/topics/user-interface-ui
     
  3. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    No motive, i prefer new UI 4.6 but i dont know how to implement it in this case, im working in a rpg click to move and i already to 2 canvas, one for inventories, and other for skill bar... but in this case i just think its easy or faster...

    i dont get any error with that code... i just wonder if i can push it a bit out of center of screen.. cuz in center it goes over the player.. just that
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    ok, now you're changing your story o_O
     
  5. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    wt i said is, if i use a float to divide the height and width i get an error... just that, like for example

    GUI.Label (new Rect (Screen.width / 2.5, Screen.height / 2.5, 1280, 800), "Press E To Interact!");

    but it will be the only way to take out the text from complete center of screen..

    if i use int number like i use i dont get error just no text where i want in any screen resolution

    GUI.Label (new Rect (Screen.width / 2, Screen.height / 2, 1280, 800), "Press E To Interact!");
     
  6. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    You have to read the error message and learn some basics. Sounds harsh, but i kindly recommend that.

    2.5 is a double precision floating point number, also called double.
    2.5f is a single precision floating point number, also called float.

    The constructor of the Rect struct that you use takes 4 float values. Append the f to the literal values or define some float variables.
     
  7. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    I get my own error last night i forgot the "f" after 2.5 -.- really my bad sry