The question is answered, right answer was accepted
Null Reference Exception when finding GameObject on Networked Game
I'm making a first person shooter using Unity Networking, which is something I've never used before, so I'm still trying to make sense of it. I have a GameObject in my Network Scene which is called DecalManager, and it is being used for the creation of things like bullet hole decals. There is a script attached to it which is a NetworkBehaviour, and contains data such as a list of all current bullet holes, to manage them and make sure there are no more than a certain amount on the server.
My issue is this; when I was using a Network Manager, it was working fine. The local players all store a reference to the servers decal manager, and when a gunshot hits a certain type of object, it calls a function in the decal manager to create a new one and the correct position. But then, I swapped it out for a Network Lobby Manager, and suddenly I'm getting a Null Reference Exception when it tries to find the reference. After some experimentation, I've determined that it is unable to find the DecalManager GameObject, even though I can definitely see that it is in the scene.
I have tried swapping the Network Lobby Manager back to a Network Manager to make sure I haven't changed anything else that would have broken it, and it started working again. So somehow, the simple act of using a Network Lobby Manager instead of a standard Network Manager has made it unable to find the object in the scene.
The relevant code in the player script, which is a Network Behaviour attached to all players:
private DecalManager decalManager;
void Start(){
//This is the line that gives the null reference exception
decalManager = GameObject.FindGameObjectWithTag("DecalManager").GetComponent<DecalManager> ();
}
The decal manager script:
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
using System.Collections.Generic;
public class DecalManager : NetworkBehaviour {
[SerializeField] private int maxBulletHoles = 100; //Max amount of decals that can exist
[SerializeField] private GameObject bhSprite; //Sprite image for bullet holes
private List<GameObject> bulletHoles = new List<GameObject>();
public void CreateBullethole(Vector3 position, Quaternion rotation)
{
if(bulletHoles.Count >= maxBulletHoles) //Has the maximum been reached?
{
NetworkServer.Destroy (bulletHoles[0]); //Destroy decal on server
Destroy (bulletHoles[0]); //Destroy object locally
bulletHoles.RemoveAt (0); //Remove from list
}
GameObject newHole = (GameObject)Instantiate (bhSprite, position, rotation); //Create new decal
float randRot = Random.Range (0, 360);
float randScale = Random.Range (0, 1);
newHole.transform.RotateAround(newHole.transform.position, newHole.transform.forward, randRot);
//TODO: MAKE DECALS APPEAR WITH SIZE VARIATIONS
newHole.transform.parent = transform; //Set parent
NetworkServer.Spawn(newHole); //Spawn on server
bulletHoles.Add (newHole); //Add to array
}
}
Does anybody know why this could be happening?
Thanks for any assistance in advance.
Update: I have also found that this error is not occurring for clients connecting to the game, only for the player that is hosting the server.
Answer by Exuro · Dec 11, 2015 at 09:45 PM
Okay, so I've figured it out, and it's exactly what I thought it was at the start. It was simply the fact that when the start function of the player was being called, the level hadn't totally loaded yet, so the object didn't exist.
I had tried to account for that before, but the error was still happening. However, I've just tried it again, and it's worked.