为什么我的单身人士空?

时间:2015-07-28 14:51:32

标签: c# unity3d multiplayer unity-networking

我尝试个性化NetworkManager,但他一直在给予

  

NullReferenceException

...在命令NetworkManager.singleton.StartHost ();

问题出在功能NetWorkManager.Singleton.StartHost();的第StartGame();行总是返回

......我不知道为什么会发生

这是代码。

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();
    }
}

它是在团结5下制作的所以我怎么能解决这个问题?

2 个答案:

答案 0 :(得分:1)

这是因为您覆盖了唤醒功能。 它没有标记,但我发现实现Awake会阻止单例的标准Unity赋值。

所以,删除你的觉醒,或者自己做单身=

答案 1 :(得分:0)

我想也许你没有初始化NetworkManager.singleton?你有没有把它作为NetworkManager的一个实例新建?