为什么我的单例是空的
本文关键字:单例 我的 为什么 | 更新日期: 2023-09-27 18:05:21
我尝试个性化的NetworkManager
,但他一直给
得到NullReferenceException
…命令NetworkManager.singleton.StartHost ();
问题在StartGame();
函数中的NetWorkManager.Singleton.StartHost();
行总是返回
…我不知道为什么会这样
代码如下:
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制作的,所以我怎么解决这个问题??
这是因为您重写了Awake函数。它没有被标记,但我发现实现Awake块的标准Unity分配的单例。
所以,删除你的Awake,或者自己设置singleton = this
我想也许你没有初始化NetworkManager.singleton?您是否曾经将它作为NetworkManager的实例添加?