error CS0103: The name `String' does not exist in the current context

I need help solving the error that is in the title.

  1. DevLogin.cs(5,35): error CS0103: The name `String’ does not exist in the current context

  2. DevLogin.cs(6,35): error CS0103: The name `String’ does not exist in the current context

    using UnityEngine;
    using System.Collections;

    public class Devlogin : MonoBehaviour {
    private string username = String.Empty;
    private string password = String.Empty;
    private bool correctLogin;

    void  Update (){
    	correctLogin = (username == "LegendWill" && password == "admin");
    }
    
    void  OnGUI (){
    	Rect windowRect;
    	windowRect.x = Screen.width / 2 - 100;
    	windowRect.y = Screen.height / 2 - 50;
    	windowRect.width = 200;
    	windowRect.height = 100;
    	GUI.Window(0, windowRect, OnWindowGUI, "Authentication");
    }
    
    void  OnWindowGUI (){
    	username = GUILayout.TextField(username);
    	password = GUILayout.PasswordField(password, '*'[0]);
    	if (GUILayout.Button("Login") && correctLogin)
    	{
    		Debug.Log("You logged in!");
    		enabled = false;
    	}
    }
    

    }

When I change it to a lowercase s, I get even more errors http://prntscr.com/4knr9q

Yes, there are further errors in the script (that weren't evaluated before, because of the error at the start): You need to initialise the windowRect: Rect windowRect = new Rect(); OnWindowGUI has to take an argument of type int: void OnWindowGUI(int id) And you can't use indexing with char - use " to make it a string: password = GUILayout.PasswordField(password, "*"[0]);

@AyAMrau, the real object is String in namespace System(which was what was missing), string is syntactic sugar for String, this goes for int which is really Int32/Int64, but they are in the System namespace and will not show unless your "using" the namespace and of course will cause an error during build.

2 Answers

2

It’s string.Empty with lowercase s

using UnityEngine;
using System.Collections;

public class Devlogin : MonoBehaviour {
    private string username = string.Empty;
    private string password = string.Empty;
    private bool correctLogin;

    void Update (){
        correctLogin = (username == "LegendWill" && password == "admin");
    }

    void  OnGUI (){
        Rect windowRect = new Rect(Screen.width / 2 - 100, Screen.height / 2 - 50, 200, 100); // You need to instantiate the rect object, constructor takes what you were setting property by property.
        GUI.Window(0, windowRect, OnWindowGUI, "Authentication");
    }

    void  OnWindowGUI (int id){
        username = GUILayout.TextField(username);
        password = GUILayout.PasswordField(password, '*');  // I have no idea what you were doing with [0].
        if (GUILayout.Button("Login") && correctLogin)
        {
            Debug.Log("You logged in!");
            enabled = false;
        }
    }
}

For some reason the indexing is used in the example for PasswordFiels in the docs: GUILayout.PasswordField(password, "*"[0]) http://docs.unity3d.com/ScriptReference/GUILayout.PasswordField.html

That code brings up another error, DevLogin.cs(15,21): error CS0123: A method or delegate Devlogin.OnWindowGUI()' parameters do not match delegate UnityEngine.GUI.WindowFunction(int)' parameters Lol I'm sorry for making this so complicated.

The method you supply needs to take an argument of type int.

I edited just after i posted this and added the correct parameters/arguments to the window func.

So what is the correct code? xD