1

I try personalized NetworkManager but he keeps giving

NullReferenceException

...in the command NetworkManager.singleton.StartHost ();

the problem is at the line NetWorkManager.Singleton.StartHost(); in the fuction StartGame(); always return

...and i have no idea why it happens

here is the code.

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

It was made at unity 5 So how i can solve that ??

user3071284
  • 6,955
  • 6
  • 43
  • 57
João Melo
  • 387
  • 4
  • 11
  • 4
    I don't think you should be deriving a new type from `NetworkManager` in the first place. –  Jul 28 '15 at 15:01
  • 1
    The singleton property is a static property in the base class. It is not clear how it is instantiated. Also it is almost sure even it is instantiated it will be an instance of the base class (NetworkManager) instead of your derived class (NetManager). Seems to wrong direction/architecture for me. – g.pickardou Jul 28 '15 at 15:24
  • Possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Dour High Arch Nov 19 '15 at 01:40

2 Answers2

1

This is because you override the Awake function. It is not marked, but I found that implementing the Awake block the standard Unity assign of the singleton.

So, remove your Awake, or do singleton = this yourself

Orion
  • 301
  • 3
  • 12
0

I think maybe you didn't initialised NetworkManager.singleton?Did you ever new it as a instance of NetworkManager?

ren boxue
  • 41
  • 5