- Home /
NullReference Exception FindLocalObject C#
I have a PlayerItem script that manages object pickup and dropping for each player across a network. When I spawn the objects to the environment, I do it via a script called ItemManager on a GameObject by the same name. That item is a prefab that is spawned via NetworkSpawn. I have inventory working for the most part. Items are properly spawned as children of the ItemManager. A user can pick up items, they're properly sorted into stacks on each user via [Client] and [Command] functions. This is all reflected as far as I can tell on both the client and server in the inspector, too.
Now, here's the problem. When a player 'drops' an item, I would like to child it back to the ItemManager gameobject. However, every effort to get the object via the netId has returned a null reference exception. I've even checked the inspector during runtime and the Network Id associated with ItemManager is the same that is output by Debug.Log.
overUI = EventSystem.current.IsPointerOverGameObject();
// Pick up item (Move this!)
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit _hit;
if(Input.GetButtonDown ("Left Click") && !overUI){
if (Physics.Raycast(ray, out _hit, 100)){
if(_hit.collider.gameObject.tag == "Item"){
Debug.Log ("Item Clicked");
itemClicked = _hit.collider.gameObject;
}else{
itemClicked = null;
}
// Drop an item
if(_hit.collider.gameObject.tag == "Ground"){
Debug.Log ("Clicked Ground");
NetworkInstanceId localItemManagerNID = GameObject.Find ("ItemManager").GetComponent<NetworkIdentity>().netId;
Debug.Log ("local Item Manager NID: " + localItemManagerNID);
Debug.Log ("Game Object: " + ClientScene.FindLocalObject(localItemManagerNID));
DropItem (GetComponent<NetworkIdentity>().netId, localItemManagerNID, _hit.point);
}
}
}
The local item manager NID shows up as "2" in the console. The game object that is found via ClientScene.FindLocalObject() is immediately returned as Null. My standard practice is then to send this information to a [Client] function followed by a [Command] function which updates syncvars on the clients. Having a value that immediately references to a null gameobject doesn't make sense and clearly wont help once it returns to the clients.
Any help would be appreciated.
Answer by UNDERHILL · Sep 21, 2016 at 04:26 PM
I think you might have a synchronicity issue. Maybe put this in an IEnumerator and yield the "LocalItemManagerNID =" ?
Your answer
Follow this Question
Related Questions
Send a file using NetworkServer class. 0 Answers
How to watch in client applications what it is happening in the server one? 0 Answers
UNET Different size of spawned object on host and client 1 Answer
Need help understanding ClientRpc from Unity Networking tutorial,Question about clientrpc 1 Answer
Networked Objects not appearing on Client when Client loads scene first 1 Answer