- Home /
Null reference exception on Screenpointtoray (Multiplayer)
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?
Answer by MILTON! · Oct 31, 2013 at 02:00 PM
This was fixed! I had to use Network.Destroy on the team that have to complete the objectives. This can be closed now.
Your answer
![](https://koobas.hobune.stream/wayback/20220613120846im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Touch controls for Pong based video games for Android 0 Answers
NullRefenceException error 1 Answer
Camera to aim at raycast 1 Answer
RPC Help FPS Multiplayer Raycast Shooting & Mouse Look 2 Answers