How to fix error CS0103 Cannot implicitly covert type 'bool' to 'string'
I have a question about Unity. I have written a script but it doesn't work. I'm trying to allow the player to enter a name and on pressing Enter the text will change and show "Hello +PlayerName" I have no idea how to fix the error error CS0103 The name `Submit' does not exist in the current context. Please explain
string PlayerName = "Player1";
string Submit = false;
void Update() { if(Input.GetKeyDown("enter")) {
Submit = true; }
}
void OnGUI () {
if (Submit)
{
GUI.Label (Rect (10, 10, 100, 30), "Hello " + PlayerName);
}
else
{
GUI.Label (Rect (10, 10, 100, 30), "Enter Name:");
PlayerName = GUI.TextField (Rect (90, 10, 200, 25), PlayerName, 40);
}
}
}
Answer by Pengocat · Jan 06, 2017 at 02:19 AM
Submit should be of type bool, not string.
bool Submit = false;
Also remember you need to new up Rect.
GUI.Label(new Rect(10, 10, 100, 30), "Hello " + PlayerName);
I did that but now i'm getting this error (18,42): error CS1502: The best overloaded method match for UnityEngine.GUI.TextField(UnityEngine.Rect, string, int)' has some invalid arguments for
PlayerName = GUI.TextField (Rect (90, 10, 200, 25), PlayerName, 40);`
you also need new before Rect in this case
PlayerName = GUI.TextField(new Rect(90, 10, 200, 25), PlayerName, 40);
Here is all of it corrected.
string PlayerName = "Player1";
bool Submit = false;
void Update()
{
if (Input.Get$$anonymous$$eyDown("enter"))
{
Submit = true;
}
}
void OnGUI()
{
if (Submit)
{
GUI.Label(new Rect(10, 10, 100, 30), "Hello " + PlayerName);
}
else
{
GUI.Label(new Rect(10, 10, 100, 30), "Enter Name:");
PlayerName = GUI.TextField(new Rect(90, 10, 200, 25), PlayerName, 40);
}
}
Your answer
Follow this Question
Related Questions
How do You add a Scrollbar in a textbox C# code 0 Answers
Connect C# script with GuiText 1 Answer
Editor Window 1 Answer
How do you show/hide textUI in Javascript? 1 Answer
Why is this List only showing one int? 0 Answers