- Home /
This question was
closed Feb 28, 2018 at 09:35 PM by
Vencarii for the following reason:
I found the solution myself
Question by
Vencarii · Feb 28, 2018 at 02:19 PM ·
networkingnullreferenceexception
How can I use a GameObject to hold all references in a network game?
I have a GameObject called AppManager and I want to hold all the references to the different GameObjects in my game to make them easily accessible from other scripts. Like this:
using UnityEngine;
using UnityEngine.Networking;
public class AppManager : NetworkBehaviour
{
[HideInInspector]
public static AppManager instance = null;
private HudController hudController;
public override void OnStartLocalPlayer()
{
base.OnStartLocalPlayer();
if (instance == null)
{
instance = this;
}
else if (instance != this)
{
Destroy(gameObject);
}
Debug.Log("OnStartLocalPlayer() " + GameObject.FindGameObjectWithTag("HUD"));
this.hudController = GameObject.FindGameObjectWithTag("HUD").GetComponent<HudController>();
}
// Getter
public HudController GetHudController()
{
return this.hudController;
}
}
Then I try to access the AppManager in another script called GameTime:
[ClientRpc]
public void RpcUpdateGameTimeOnClients(int quarterHours, int gameDay)
{
string gameTimeText;
// do some stuff to get a proper string...
// this line throws a NullReferenceException
AppManager.instance.GetHudController().UpdateGameTime(gameTimeText);
// but when I access the components directly and do
// the same stuff like the UpdateGameTime() method it works
GameObject.FindGameObjectWithTag("GameTimeText").GetComponent<Text>().text = gameTimeText;
Debug.Log(GameObject.FindGameObjectWithTag("GameManager"));
}
So I wonder why I can't get an instance of AppManager in my GameTime script? There should be an instance of AppManager on the server as well. I didn't check neither "Server Only" nor "Local Layer Authority" in the NetworkIdentity of AppManager.
Comment