0

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!

AustinWBryan
  • 2,968
  • 3
  • 17
  • 35
  • Uncomment the code, run it again then double-click on the error. It will take you to where the problem is happening. Post that line of code – Programmer Jun 02 '18 at 21:08
  • @Programmer the bit of code throwing the error is already in the question. It's the bit of code in `UI.cs` called `public static void PrintSpooler(string target, string message)` – Lv_InSaNe_vL Jun 02 '18 at 21:29
  • No. Save the code. Run it and let the error come up again. Double click on that it will show you the **line of code** not the function name, That line of code is what I am looking for – Programmer Jun 02 '18 at 21:47
  • @Programmer the line of code is `currentWrite = GameObject.Find(target).GetComponent();` is the exact line of code. – Lv_InSaNe_vL Jun 02 '18 at 21:48
  • Ok Add `Debug.Log(target)` followed by `Debug.Log(GameObject.Find(target))` before `GameObject.Find(target).GetComponent();`. Tell me the out put of both `Debug.Log` – Programmer Jun 02 '18 at 21:52
  • @Programmer So it's returning null on the GameObject part. – Lv_InSaNe_vL Jun 02 '18 at 23:05
  • Well I asked you what both of them returns. Check whatever `target` returns and then look in the Scene in the Hierarchy tab and make sure there is a GameObject with that name. If there is a GameObject with that name then make sure it is activated not deactivated. Good luck! – Programmer Jun 02 '18 at 23:09

0 Answers0