null reference help. i fundamentally dont understand.
when trying to build my menu screen, i keep recieving null reference exceptions. it occurs when i press one of the two buttons on my screen. the two buttons controller whether im trying to register or log in. here is my code: i get the errors at mainmenu line 62 and login line 37
Main Menu Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MainMenu : MonoBehaviour {
private Register register;
private LogIn login;
public Button openRegister, openLog, registerButton, LoginButton;
public InputField email, password, newEmail, newPassword, confirm;
public GameObject game;
private string em, pass, newEm, newPass, conf;
//leave the error texts and stuff for now
// Use this for initialization
void Start () {
Register register = game.GetComponent<Register>();
LogIn login = game.GetComponent<LogIn>();
//set the buttons from the log and register scripts
login.initButtons();
register.initButtons();
}
// Update is called once per frame
void Update () {
em = email.text;
pass = password.text;
newEm = newEmail.text;
newPass = password.text;
conf = confirm.text;
openLog.onClick.AddListener(openLogClick);
openRegister.onClick.AddListener(openRegisterClick);
}
void setStrings()
{
em = email.text;
pass = password.text;
newEm = newEmail.text;
newPass = password.text;
conf = confirm.text;
}
void openLogClick()
{
login = new LogIn();
login.logIn(em, pass);
}
void openRegisterClick()
{
register.registerUser(newEm, newPass, conf);
}
}
Login Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Firebase.Auth;
using UnityEngine.SceneManagement;
public class LogIn : MonoBehaviour {
public InputField emailIn, passwordIn;
public Button signInButton, openLogIn;
public Text emailErrorText, passwordErrorText, successText;
public GameObject eI, pI, sIButton, oLButton;
private string emailString, passwordString;
private FirebaseAuth auth;
private bool userWantsLog = false , isButton = false;
private Register checkUser;
private Users user;
private void Start()
{
emailErrorText.text = "";
passwordErrorText.text = "";
Register checkUser = GetComponent<Register>();
Users user = GetComponent<Users>();
initButtons();
auth = FirebaseAuth.DefaultInstance;
}
public void logIn(string email, string password)
{
if(checkUser.UserChecker(email, password))
{
SceneManager.LoadScene(1);
}
}
public void setAcctive()
{
eI.SetActive(true);
pI.SetActive(true);
sIButton.SetActive(true);
oLButton.SetActive(false);
}
public void initButtons()
{
eI.SetActive(false);
pI.SetActive(false);
sIButton.SetActive(false);
oLButton.SetActive(true);
}
void getStrings()
{
emailString = emailIn.text;
passwordString = passwordIn.text;
}
void clickEvent()
{
isButton = true;
}
void openLogClick()
{
userWantsLog = true;
}
void log(string email, string password) {
auth.SignInWithEmailAndPasswordAsync(email, password).ContinueWith(task => {
if (task.IsCanceled)
{
Debug.LogError("SignInWithEmailAndPasswordAsync was canceled.");
return;
}
if (task.IsFaulted)
{
Debug.LogError("SignInWithEmailAndPasswordAsync encountered an error: " + task.Exception);
return;
}
Firebase.Auth.FirebaseUser newUser = task.Result;
Debug.LogFormat("User signed in successfully: {0} ({1})",
newUser.DisplayName, newUser.UserId);
});
}
}
Register Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Firebase.Unity.Editor;
using UnityEngine.UI;
using Firebase.Auth;
using System.Linq;
using UnityEngine.SceneManagement;
public class Register : MonoBehaviour
{
public InputField Password, Email, Confirm;
public Text success, errorText;
public Button registerButton, openRegister;
public GameObject nEM, nP, nCP, SU, registerObject;
private Scene scene;
private string email, password, conf;
private bool successToken = false;
private bool isRegister;
private int userCount;
private Users user;
private FirebaseAuth auth;
private SceneChanger change;
private Users[] userArray;
Scene currentScene;
// Use this for initialization
void Start()
{
isRegister = false;
change = GetComponent<SceneChanger>();
currentScene = SceneManager.GetActiveScene();
userCount = 0;
userArray = new Users[100];
auth = FirebaseAuth.DefaultInstance;
success.text = "";
errorText.text = "";
}
public void registerUser(string email, string password, string confirm)
{
this.email = email;
this.password = password;
conf = confirm;
//check whether the password meets criteria and whether they match
checkPass();
checkConfirm();
if(checkPass() && checkConfirm())
{
if (UserChecker(this.email, this.password))
{
addUser();
}
}
}
void setActive()
{
nEM.SetActive(true);
nP.SetActive(true);
nCP.SetActive(true);
SU.SetActive(true);
}
public void initButtons()
{
nEM.SetActive(false);
nP.SetActive(false);
nCP.SetActive(false);
SU.SetActive(false);
registerObject.SetActive(true);
}
bool checkConfirm()
{
if (conf == password)
return true;
else
return false;
}
bool checkPass()
{
if (password.Length >= 6 && password.Any(char.IsUpper) && password.Any(char.IsDigit)){
return true;
}
else return false;
}
bool registerSuccessToken()
{
if (checkConfirm() && checkPass())
return true;
return false;
}
void signUp(string email, string password)
{
auth.CreateUserWithEmailAndPasswordAsync(email, password).ContinueWith(task => {
if (task.IsCanceled)
{
Debug.LogError("CreateUserWithEmailAndPasswordAsync was canceled.");
return;
}
if (task.IsFaulted)
{
Debug.LogError("CreateUserWithEmailAndPasswordAsync encountered an error: " + task.Exception);
return;
}
// Firebase user has been created.
Firebase.Auth.FirebaseUser newUser = task.Result;
successToken = true;
Debug.LogFormat("Firebase user created successfully: {0} ({1})",
newUser.DisplayName, newUser.UserId);
});
}
void addUser()
{
user = new Users(email, password);
userArray[userCount] = user;
userCount++;
Debug.Log(user.retEmail());
Debug.Log("if there arent");
}
void registerB()
{
isRegister = true;
nEM.SetActive(true);
nP.SetActive(true);
nCP.SetActive(true);
SU.SetActive(true);
registerObject.SetActive(false);
}
void singUpClicked()
{
Debug.Log("enters if. creating user");
signUp(email, password);
isRegister = false;
setActive();
registerObject.SetActive(true);
addUser();
}
public bool UserChecker(string em, string pass)
{
//string emailHold, passHold;
if (userArray.Length == 0)
{
errorText.text = "no users exist";
return true;
}
else
{
for (int i = 0; i < userArray.Length; i++)
{
Debug.Log(userArray[i].retEmail());
Debug.Log(userArray[i].retPass());
if (userArray[i].retEmail() == em && userArray[i].retPass() == pass)
return true;
}
return false;
}
}
}
someone please help. i do not fundamentally get how to reference the other scripts.
Answer by Frederikw · Apr 06, 2020 at 08:47 PM
I know its an old question, but I have the exact same problem. Any solutions?
Your answer
Follow this Question
Related Questions
#Need Fast Help# NullReferenceException: Object reference not set to an instance of an object 0 Answers
Any other ways for instantiated prefabs to reference game-objects in the scene? 0 Answers
How do I reference an Instantiate object? 1 Answer
Access variables or methods from another class (or gameObject) [C#] 1 Answer