VOid OnGUI on a specific place in a panel

Hi everybody !

I’m using a InputInField to do a chat in Multiplayer, But when i’m using the metod on, my text he’s not on the left corner down of my panel, he’s on the left corner of my screen :confused:

How can i attach it to my panel ?

here my code :

        void OnGUI()
        {
            GUILayout.BeginArea(new Rect(0, 0, Screen.width, Screen.height));
            GUILayout.BeginVertical();
            GUILayout.FlexibleSpace();
            guiStyle.fontSize = 30; //change the font size
            foreach (string msg in chatMessages)
            {
    
                GUILayout.Label(msg, guiStyle);
            }

            GUILayout.EndVertical();
            GUILayout.EndArea();
        }

Thx for reading me and have a good day ! :slight_smile:

Are you using a Unity UI panel and then trying to use OnGUI for your text?

Yes! I create a UI panel "Xpanel " , in this panel i have my InputInfiled with the script and Fonction “OnGui”

But my text appear not on the corner of my panel, but on the corner of the screen :confused:

I’ll be honest, I haven’t used OnGUI in ages. And I have never tried to mix OnGui with Unity UI system. Why aren’t you just using the Unity UI text and inputField? Or if you want fancier, you can get TextMesh pro free now which has text and inputfield of it’s own?

I will say it may be harder to find people using the OnGUI system as many people have switched over from what I’m seeing.

I’m sure for your use it will require some sort of layout for the textbox to be positioned correctly, but just sounds like a pain to set up.

To be honest, i do like this because i follow a tutorial on internet ^^

So i think that the best way is to change my metod to UI text like you said !
how can i replace onGui by UI text to wrote on the screen my message ? I instanciate a prefab text for each message ?

Yes, you can instantiate next UI Text objects each time.
Some other options would be: append to (one) UI Text Object, or use a custom inputfield that acts like an output only. (just threw that in there as an idea I once used).

Yes, there are different ways of doing this. You could create a scrollrect with a layout group and just create text objects for each message (just make sure you cap it so you don’t end up with hundreds of messages.) Or you can have one text object and append text to it as @methos5k mentioned, though you’ll want to cap that also and cut off older messages so it doesn’t get to long as well…

There may be other things to consider depending on if you want to do more with the text than just display it.