Hey all. In my multiplayer game of Hunter vs Hunted the hunted team have to complete objectives. I set up the following script for this…
void Update () {
if (Input.GetButtonDown("Fire2")){
NetworkViewID viewID = Network.AllocateViewID();
networkView.RPC("RaycastObjective",RPCMode.AllBuffered, viewID);
}
}
[RPC]
void RaycastObjective(NetworkViewID viewID){
RaycastHit ObjectiveHit = new RaycastHit();
Ray objRay = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(objRay, out ObjectiveHit, 400))
{
if (ObjectiveHit.transform.name == "IDCard")
{
currentobjective = 1;
Debug.Log("Hit Keycard!");
Destroy(IDCard);
}
if (ObjectiveHit.transform.name == "Generator")
{
if (currentobjective == 1)
{
Debug.Log("Generator Activated!");
currentobjective = 2;
}
}
}
The problem is that when the Hunter team spawns they get this error…
NullReferenceException
UnityEngine.Camera.ScreenPointToRay (Vector3 position) (at C:/BuildAgent/work/ea95e74f6e5f192d/Runtime/ExportGenerated/Editor/UnityEngineCamera.cs:379)
Obj_Sketchup.RaycastObjective (NetworkViewID viewID) (at Assets/Scripts/MapScripts/Obj_Sketchup.cs:50)
UnityEngine.NetworkView:RPC(String, RPCMode, Object[])
Obj_Sketchup:Update() (at Assets/Scripts/MapScripts/Obj_Sketchup.cs:32)
From what this error says it means that the lines responsible for the error are…
networkView.RPC("RaycastObjective",RPCMode.AllBuffered, viewID);
and…
Ray objRay = Camera.main.ScreenPointToRay(Input.mousePosition);
I can’t for the life of me work out why I would be getting a null reference exception error? I have all cameras tagged properly and it seems odd that only one team are getting the error. What would you suggest for a work around?