I have three scripts; Player.cs
which controls the player's movement, camera direction and collision trigger events, MainMenu.cs
which controls all the menus in the game, and UI.cs
which controls all the UI in the game.
In UI.cs
I have the following method;
public static void PrintSpooler(string target, string message)
{
currentWrite = GameObject.Find(target).GetComponent<Text>();
currentWrite.text = message;
}
this was designed to simplify the process of printing things out on the screen by just having a single method I can call from anywhere in the program, which I do from 'Player.cs' and 'MainMenu.cs'.
The call in MainMenu.cs
looks like this;
public void Host()
{
string localComputerName = Dns.GetHostName();
IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());
string hostMessage = "Game hosted at " + localIPs[3];
Time.timeScale = 1;
SceneManager.LoadScene("TestScene2");
UI.PrintSpooler("GameKills", hostMessage);
}
What is supposed to happen is when the player chooses to host a game, it should print out the IP address the game is hosted on so other players can join the game. What actually happens is I get an error message saying "NullReferenceException: Object reference not set to an instance of an object" which points to the bit of code in 'UI.cs'. The really weird thing is that if you comment out the method call in 'MainMenu.cs' to allow the game to continue the following call in 'Player.cs' works;
else if (other.GetComponent<Trap>())
{
string killed = player + " killed by " + other.GetComponent<Trap>().Owner + "'s " + other.GetComponent<Trap>().name;
UI.PrintSpooler("GameKills", killed);
}
Which as far as I can tell is, in essence, the exact same call, just from two different locations. What I think is happening is that since the scenes switch from a scene called "JoinGame" to "TestScene2" (which is the actual scene where the player would play the game, Unity is losing track of what the code is doing to what part of the code.
But of course, I can't figure it out for the life of me so if you guys could help that would be amazing! Feel free to ask for clarification on anything you need!
Thanks, guys!