0

我正在为 Unity 创建一个帐户系统,这是我的脚本,在 TextField 和 PasswordFields 之前的第 132、134、136 行出现错误。我说不出它有什么问题。任何帮助将不胜感激。

using UnityEngine;
using System.Collections;

public class menuManager : MonoBehaviour {

    public string currentMenu;

    public string Username;
    public string Password;

    private string userTre = "holybuttwipe";
    private string passTre = "tre";

    private string userDerek = "Prouda You";
    private string passDerek = "derek";

    private string userBodey = "cherrypepsi72";
    private string passBodey = "bodey";

    private string userCody = "Bear On The Moon";
    private string passCody = "cody";

    private bool tre = false;
    private bool derek = false;
    private bool bodey = false;
    private bool cody = false;



    void Start() {
        currentMenu = "Main";
    }

    void Update() {

    }

    void OnGUI() {
        if(currentMenu == "Main")
            Menu_Main();
        if(currentMenu == "Login")
            Menu_Login();
        if(currentMenu == "Game")
            Menu_Game();
        if(currentMenu == "Create")
            Menu_Create();
        GUI.Label(new Rect(100, 100, 100, 100), Username);
    }

    public void NavigateTo(string nextmenu) {
        currentMenu = nextmenu;
    }

    public void Menu_Main() {
        if(GUI.Button(new Rect(10, 10, 300, 50), "Login")) {
            NavigateTo("Login");
        }
        if(GUI.Button(new Rect(10, 60, 300, 50), "Create Account")) {
            NavigateTo("Create");
        }
    }

    public void Menu_Login() {
        GUI.Label(new Rect(10, 10, 100, 30), "Username");
        Username = GUI.TextField(new Rect(110, 10, 100, 30), Username);
        GUI.Label(new Rect(10, 50, 100, 30), "Username");
        Password = GUI.PasswordField(new Rect(110, 50, 100, 30), Password, '*');
        if(GUI.Button(new Rect(10, 85, 210, 30), "Login")) {
            if(Username == userTre) {
                if(Password == passTre) {
                    NavigateTo("Game");
                    tre = true;
                }
                else {
                    Username = "";
                    Password = "";
                }
            }
            else if(Username == userDerek) {
                if(Password == passDerek) {
                    NavigateTo("Game");
                    derek = true;
                }
                else {
                    Username = "";
                    Password = "";
                }
            }
            else if(Username == userBodey) {
                if(Password == passBodey) {
                    NavigateTo("Game");
                    bodey = true;
                }
                else {
                    Username = "";
                    Password = "";
                }
            }
            else if(Username == userCody) {
                if(Password == passCody) {
                    NavigateTo("Game");
                    cody = true;
                }
                else {
                    Username = "";
                    Password = "";
                }
            }
            else {
                Username = "";
                Password = "";
            }
        }
    }

    public void Menu_Game() {
        GUI.Label(new Rect(10, 10, 300, 30), "You are logged in as " + Username);
        if(GUI.Button(new Rect(10, 50, 300, 50), "Logout")) {
            NavigateTo("Main");
            tre = false;
            derek = false;
            bodey = false;
            cody = false;
        }
    }

    public void Menu_Create() {
        string newUsername;
        string newPassword;
        string conPassword;
        GUI.Label(new Rect(10, 10, 100, 30), "Username");
        newUsername = GUI.TextField(new Rect(100, 10, 100, 30), newUsername);
        GUI.Label(new Rect(10, 50, 100, 30), "Password");
        newPassword = GUI.PasswordField(new Rect(100, 50, 100, 30), newPassword, '*');
        GUI.Label(new Rect(10, 90, 100, 30), "Confirm Password");
        conPassword = GUI.PasswordField(new Rect(100, 90, 100, 30), conPassword, '*');
        if(GUI.Button(new Rect(10, 130, 200, 30), "Create Account")) {
            if(newPassword == conPassword && newUsername != userTre && newUsername != userDerek && newUsername != userBodey && newUsername != userCody) {
                Username = newUsername;
                Password = newPassword;
                NavigateTo("Login");
            }
            else {
                newUsername = "";
                newPassword = "";
                conPassword = "";
            }
        }
    }
}
4

2 回答 2

0

我怀疑你的问题如下:

newUsername = GUI.TextField(new Rect(100, 10, 100, 30), newUsername);

在为函数分配任何值之前,您正在使用 newUsername 作为函数的参数。当然,它会在 AFTERWARD 之后有一个价值,但它不能走那么远。

当然,您要使用不同的变量执行此操作 3 次。

于 2013-10-10T16:15:49.423 回答
0
newUsername = GUI.TextField(new Rect(100, 10, 100, 30), newUsername);

newPassword = GUI.PasswordField(new Rect(100, 50, 100, 30), newPassword, '*');

conPassword = GUI.PasswordField(new Rect(100, 90, 100, 30), conPassword, '*');

在这里,您正在使用newUsernamenewPassword并且conPassword没有对它们应用任何值。您需要为它们分配一个字符串值,并将before它们作为参数传递。

或者,您可以像这样声明它们:

string newUsername = "";
string newPassword = "";
string conPassword = "";
于 2013-10-10T16:16:38.933 回答