Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Question My GUI.Label does not work and I do not know what to do.

Discussion in 'Immediate Mode GUI (IMGUI)' started by nidlatam, Feb 13, 2023.

  1. nidlatam

    nidlatam

    Joined:
    Feb 13, 2023
    Posts:
    3
    This code is attached to a square sprite. I have no other scripts. I am trying to get GUI.Label to work for future use. It does not display the text.

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3.  
    4. public class SquareScript : MonoBehaviour
    5. {
    6.     //float accel = 1f;
    7.  
    8.     void Start()
    9.     {
    10.         //transform.position = new Vector3(1, 0);
    11.     }
    12.  
    13.     void Update()
    14.     {
    15.         //transform.position = new Vector3((float)Math.Cos(Math.Pow(Time.time, 2) * accel / 2), (float)Math.Sin(Math.Pow(Time.time, 2) * accel / 2));
    16.     }
    17.  
    18.     void OnGUI()
    19.     {
    20.         GUI.Label(new Rect(-1f, -1f, 2f, 2f), "Where is the text?");
    21.     }
    22.  
    23. }
    By the way I get this warning but I had problems before having this warning so I doubt it has anything to do with this. I should probably put this second part in a seperate question, but because there is a slight chance it might have something to do with this I will include it here.

    Unable to find player assembly: C:\Users\User\My project\Temp\StagingArea\Data\Managed\UnityEngine.TestRunner.dll
    UnityEngine.Debug:LogWarning (object)
    Unity.Burst.Editor.BurstAotCompiler:OnPostBuildPlayerScriptDLLsImpl (UnityEditor.Build.Reporting.BuildReport) (at Library/PackageCache/com.unity.burst@1.6.6/Editor/BurstAotCompiler.cs:350)
    Unity.Burst.Editor.BurstAotCompiler:OnPostBuildPlayerScriptDLLs (UnityEditor.Build.Reporting.BuildReport) (at Library/PackageCache/com.unity.burst@1.6.6/Editor/BurstAotCompiler.cs:208)
    UnityEditor.BuildPlayerWindow:BuildPlayerAndRun ()
     
  2. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    726
    Isn't that going to give you a 2 pixel by 2 pixel rect that's centered on the top-left corner of the screen?
     
  3. nidlatam

    nidlatam

    Joined:
    Feb 13, 2023
    Posts:
    3
    Are you trying to say that it is too small to have text or something?
     
  4. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    726
    Yes. All of the GUI methods work in GUI Space. (0,0) is the top left corner, and the bottom right corner is (screenWidth, screenHeight).

    You should see the text if you do something like GUI.Label(new Rect(0, 0, 500, 500), "Hello!")

    Do you want a piece of text to appear next to the sprite? If so, I would just use a TextMeshPro object parented to the sprite.
     
  5. nidlatam

    nidlatam

    Joined:
    Feb 13, 2023
    Posts:
    3
    Wait so the coordinates are monitor coordinatones and not unity coordinates? That makes so much sense!
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,761