【发布时间】:2020-03-14 05:29:27
【问题描述】:
在 unity 2019 中,我们如何将一个输入字段文本传输到另一个类标签或输入字段文本?我已经尝试过继承并使我的变量也公开。但是没用。
我的代码没有显示任何错误但没有传输值,运行时显示空值错误。 当我运行代码时,它在 SessionData 类 UserNameTextLabel.text 中显示 NullObjectRefrence 错误;和 UserIDTextLabel.text。
public class LogIn : MonoBehaviour
{
public InputField UsernameTXTBOX;
public InputField PasswrodTXTBOX;
public TMPro.TMP_Text UserNameTextLabel;
public TMPro.TMP_Text UserIDTextLabel;
public int UserID;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void SceneSwitcher(int SceneIndex)
{
SceneManager.LoadScene(SceneIndex);
}
public void Verify()
{
if (UsernameTXTBOX.text.Equals("Abc") && PasswrodTXTBOX.text.Equals("def"))
{
Debug.Log(UsernameTXTBOX.text);
// Session["Data"] = UserNameIDTXTBOX.Text;
UserID = 1234;
SceneManager.LoadScene(0);
}
else if (UsernameTXTBOX.text.Equals("Teacher") && PasswrodTXTBOX.text.Equals("1234"))
{
Debug.Log(UsernameTXTBOX.text);
UserID = 1234;
SceneManager.LoadScene(0);
}
else if (UsernameTXTBOX.text.Equals("Def") && PasswrodTXTBOX.text.Equals("0000"))
{
SceneManager.LoadScene(3);
}
else
{
// UnityEditor.EditorUtility.DisplayDialog("Warning","Invalid Ceredentials","Close");
} } }
public class SessionData : Monobehaviour
{
LogIn L = new LogIn();
public TMPro.TMP_Text UserNameTextLabel;
public TMPro.TMP_Text UserIDTextLabel;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
// Debug.Log(UsernameTXTBOX.text);
UserNameTextLabel.text = UsernameTXTBOX.text;
UserIDTextLabel.text = UserID.ToString();
} }
【问题讨论】:
-
这是两个不同的类吗?如果是这样,
SessionData应该如何知道UserID?好像没有这样的成员。您能否准确描述您希望从哪里传递到哪里的数据? -
您能否将该代码部分分成多个部分,例如通过将类彼此分开,以便读者可以轻松阅读和理解您的代码。还要解释两个类之间的关系以便更好地理解。
-
-
@derHugo LogIn L = new LogIn();我在 SessionData 类中创建了一个对象,但没有帮助我将一个类文本框值转移到另一个类标签文本。