- Home /
Get user's name in Unity using GUILayout.TextField()
I'm a beginner in unity and I want to get a user's name after my game ends, so I can save his score. To do this, I found out you use GUILayout.TextField(). I looked it up and made a C# script using it but for some kind of reason it gives me a compiler error. Here is the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NameGUI : MonoBehaviour {
public string PlayerName = "Name Here";
void OnGUI(){
PlayerName = GUILayout.TextField (new Rect(0, 0, 200, 20), PlayerName);
}
}
And it gives me this error message:
*The best overloaded method match for 'UnityEngine.GUILayout.TextField(string, params UnityEngine.GUILayoutOption[])' has some invalid arguments
Argument 1: Cannot convert from 'UnityEngine.Rect to string'
Argument 2: Cannot convert from 'string' to 'UnityEngine.GUILayoutOption[]'*
I would really appreciate your help. If you have any questions, you can simply ask.
Answer by Bunny83 · Aug 19, 2017 at 04:13 PM
When you use any method of the Unity API you should first check the documentation which usually list all overloads with their parameters. Or just use a proper IDE (like Visual Studio) which tells you right away what overloads are available once you write the opening bracket:
So just use:
PlayerName = GUILayout.TextField(PlayerName);
However if you prefer to specify the Rect manually, you have to use the GUI version:
PlayerName = GUI.TextField (new Rect(0, 0, 200, 20), PlayerName);
Answer by PascalGamingYT · Aug 20, 2017 at 11:32 AM
It worked! Sorry for taking your time but thank you.
Your answer
Follow this Question
Related Questions
How To Scale GUI Based On Screen Resolution 1 Answer
Detect OnMouseEnter/Exit/Down on childrens 0 Answers
My player still keep moving when i unparent it form the car 2 Answers
How do you CORRECTLY call methods from another C# file 1 Answer
[Help] hey i'm just a beginner... Problem with WEB GL build 0 Answers