为什么我的单例为空?
Why is my singleton null?
我尝试个性化 NetworkManager
但他一直给
NullReferenceException
...在命令中 NetworkManager.singleton.StartHost ();
问题出在函数 StartGame();
中的行 NetWorkManager.Singleton.StartHost();
总是 return
...我不知道为什么会这样
这是代码。
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
using UnityEngine.UI;
public class NetManager : NetworkManager {
public Transform Bombeiro, Cnen, Policial, Suspeito,IaTeamPlayer;
public string ServidorIP, NomeUsuario;
private GameObject WayPoint;
public int GrupoAvatar;
public bool IniciarServidor;
public GameObject Lacamera,menu;
private Transform IA;
private MandaRad recptor;
private SobeRampa CodigoSubida;
public GameObject[] BombeiroSpawnPoints;
public GameObject[] PolicialSpawnPoints;
public GameObject[] SuspeitoSpawnPoints;
public GameObject[] CnenSpawnPoints;
private GameObject[] pedestres;
GameObject spawnPos;
void Awake()
{
BombeiroSpawnPoints = GameObject.FindGameObjectsWithTag("spawnBombeiroTeam");
PolicialSpawnPoints = GameObject.FindGameObjectsWithTag("spawnPolicialTeam");
SuspeitoSpawnPoints = GameObject.FindGameObjectsWithTag ("spawnSuspeitoTeam");
CnenSpawnPoints = GameObject.FindGameObjectsWithTag ("spawnCnemTeam");
}
public void ServerIni (bool iniciar)
{
IniciarServidor = iniciar;
}
public void PegaCampoIP(GameObject IP)
{
if (IP.GetComponent<InputField> ().text == "")
ServidorIP = IP.GetComponent<InputField> ().placeholder.GetComponent<Text> ().text;
else
ServidorIP = IP.GetComponent<InputField> ().text;
}
public void PegaCampoNome(GameObject Nome)
{
if (Nome.GetComponent<InputField> ().text == "")
NomeUsuario = Nome.GetComponent<InputField> ().placeholder.GetComponent<Text> ().text;
else
NomeUsuario = Nome.GetComponent<InputField> ().text;
}
public void EscolherAvatar (int grupo)
{
if (GrupoAvatar == 0)
{
spawnPos = BombeiroSpawnPoints[Random.Range(0, BombeiroSpawnPoints.Length)];
base.playerPrefab = Bombeiro.gameObject;
base.spawnPrefabs.RemoveAt(0);
base.spawnPrefabs.Add(spawnPos);
}
else if (GrupoAvatar == 1)
{
spawnPos = CnenSpawnPoints[Random.Range(0, CnenSpawnPoints.Length)];
base.playerPrefab = Cnen.gameObject;
base.spawnPrefabs.RemoveAt(0);
base.spawnPrefabs.Add(spawnPos);
}
else if (GrupoAvatar == 2)
{
spawnPos = PolicialSpawnPoints[Random.Range(0, PolicialSpawnPoints.Length)];
base.playerPrefab = Policial.gameObject;
base.spawnPrefabs.RemoveAt(0);
base.spawnPrefabs.Add(spawnPos);
}
else if (GrupoAvatar == 3)
{
spawnPos = SuspeitoSpawnPoints[Random.Range(0, SuspeitoSpawnPoints.Length)];
base.playerPrefab = Suspeito.gameObject;
base.spawnPrefabs.RemoveAt(0);
base.spawnPrefabs.Add(spawnPos);
}
if (IniciarServidor)
StartGame ();
else
JoinGame ();
}
public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
{
GameObject player = (GameObject)Instantiate (base.playerPrefab, spawnPos.transform.position, spawnPos.transform.rotation);
NetworkServer.AddPlayerForConnection (conn, player, playerControllerId);
}
public void StartGame()
{
NetworkManager.singleton.StartHost();
}
public void JoinGame()
{
NetworkManager.singleton.networkAddress = ServidorIP;
NetworkManager.singleton.StartClient();
}
}
它是在 unity 5 上制作的,那我该如何解决呢??
我想你可能没有初始化 NetworkManager.singleton?你有没有将它作为 NetworkManager 的一个实例来新建?
这是因为您覆盖了唤醒功能。
它没有标记,但我发现实现 Awake 会阻止单例的标准 Unity 分配。
所以,删除你的 Awake,或者自己做 singleton = this
我尝试个性化 NetworkManager
但他一直给
NullReferenceException
...在命令中 NetworkManager.singleton.StartHost ();
问题出在函数 StartGame();
中的行 NetWorkManager.Singleton.StartHost();
总是 return
...我不知道为什么会这样
这是代码。
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
using UnityEngine.UI;
public class NetManager : NetworkManager {
public Transform Bombeiro, Cnen, Policial, Suspeito,IaTeamPlayer;
public string ServidorIP, NomeUsuario;
private GameObject WayPoint;
public int GrupoAvatar;
public bool IniciarServidor;
public GameObject Lacamera,menu;
private Transform IA;
private MandaRad recptor;
private SobeRampa CodigoSubida;
public GameObject[] BombeiroSpawnPoints;
public GameObject[] PolicialSpawnPoints;
public GameObject[] SuspeitoSpawnPoints;
public GameObject[] CnenSpawnPoints;
private GameObject[] pedestres;
GameObject spawnPos;
void Awake()
{
BombeiroSpawnPoints = GameObject.FindGameObjectsWithTag("spawnBombeiroTeam");
PolicialSpawnPoints = GameObject.FindGameObjectsWithTag("spawnPolicialTeam");
SuspeitoSpawnPoints = GameObject.FindGameObjectsWithTag ("spawnSuspeitoTeam");
CnenSpawnPoints = GameObject.FindGameObjectsWithTag ("spawnCnemTeam");
}
public void ServerIni (bool iniciar)
{
IniciarServidor = iniciar;
}
public void PegaCampoIP(GameObject IP)
{
if (IP.GetComponent<InputField> ().text == "")
ServidorIP = IP.GetComponent<InputField> ().placeholder.GetComponent<Text> ().text;
else
ServidorIP = IP.GetComponent<InputField> ().text;
}
public void PegaCampoNome(GameObject Nome)
{
if (Nome.GetComponent<InputField> ().text == "")
NomeUsuario = Nome.GetComponent<InputField> ().placeholder.GetComponent<Text> ().text;
else
NomeUsuario = Nome.GetComponent<InputField> ().text;
}
public void EscolherAvatar (int grupo)
{
if (GrupoAvatar == 0)
{
spawnPos = BombeiroSpawnPoints[Random.Range(0, BombeiroSpawnPoints.Length)];
base.playerPrefab = Bombeiro.gameObject;
base.spawnPrefabs.RemoveAt(0);
base.spawnPrefabs.Add(spawnPos);
}
else if (GrupoAvatar == 1)
{
spawnPos = CnenSpawnPoints[Random.Range(0, CnenSpawnPoints.Length)];
base.playerPrefab = Cnen.gameObject;
base.spawnPrefabs.RemoveAt(0);
base.spawnPrefabs.Add(spawnPos);
}
else if (GrupoAvatar == 2)
{
spawnPos = PolicialSpawnPoints[Random.Range(0, PolicialSpawnPoints.Length)];
base.playerPrefab = Policial.gameObject;
base.spawnPrefabs.RemoveAt(0);
base.spawnPrefabs.Add(spawnPos);
}
else if (GrupoAvatar == 3)
{
spawnPos = SuspeitoSpawnPoints[Random.Range(0, SuspeitoSpawnPoints.Length)];
base.playerPrefab = Suspeito.gameObject;
base.spawnPrefabs.RemoveAt(0);
base.spawnPrefabs.Add(spawnPos);
}
if (IniciarServidor)
StartGame ();
else
JoinGame ();
}
public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
{
GameObject player = (GameObject)Instantiate (base.playerPrefab, spawnPos.transform.position, spawnPos.transform.rotation);
NetworkServer.AddPlayerForConnection (conn, player, playerControllerId);
}
public void StartGame()
{
NetworkManager.singleton.StartHost();
}
public void JoinGame()
{
NetworkManager.singleton.networkAddress = ServidorIP;
NetworkManager.singleton.StartClient();
}
}
它是在 unity 5 上制作的,那我该如何解决呢??
我想你可能没有初始化 NetworkManager.singleton?你有没有将它作为 NetworkManager 的一个实例来新建?
这是因为您覆盖了唤醒功能。 它没有标记,但我发现实现 Awake 会阻止单例的标准 Unity 分配。
所以,删除你的 Awake,或者自己做 singleton = this