During a C# script, I have 4 elements that should be referenced, however I’m getting a null reference exception. I can disable the code that finds the UI elements on the objects and drag the objects into the public fields manually and it works, however this needs to be done dynamically. The 2nd error comes from the Packet.send down at the bottom. This also returns a null reference exception despite the fact that I have set up the packet script EXACTLY to the word as the tutorial shows. The opcode for the packet is 1 in both the packet script and this one, so I don’t understand where the NRE is coming from in the code. I should assume it’s because it’s not referencing the UI elements above so it can’t get the text from them. Can anybody see why I’m unable to access these elements? If I put a dot after .GetComponent(Canvas) I get access to the canvas variables such as Render Mode, etc. Also I’ve quadrouple-checked the names of all the objects in the game and checked that when it runs the components are enabled. This is a real mystery to me.
This is the code:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using ogclient_framework;
public class InterfaceManager : MonoBehaviour {
public static InterfaceManager Singleton { get; private set; }
public Canvas login_canvas;
public InputField login_input_username;
public InputField login_input_password;
public Button login_button;
void Awake()
{
if(Singleton == null)
{
DontDestroyOnLoad(this);
}
else
{
DestroyImmediate(this);
}
}
void Start()
{
InterfaceManager.Singleton.login_canvas = GameObject.Find("login_canvas").GetComponent<Canvas> ();
InterfaceManager.Singleton.login_input_username = GameObject.Find("login_input_username").GetComponent<InputField> ();
InterfaceManager.Singleton.login_input_password = GameObject.Find("login_input_password").GetComponent<InputField> ();
InterfaceManager.Singleton.login_button = GameObject.Find("login_button").GetComponent<Button> ();
}
void OnLevelWasLoaded(int level)
{
switch(level)
{
case 1:
break;
}
}
public void attemptLogin(){
Packet.send(PacketType.TCP, 1, InterfaceManager.Singleton.login_input_username.text, InterfaceManager.Singleton.login_input_password.text);
}
}