- Home /
[SOLVED] Multiplayer, RaycastHit Scene-Object returns Null Ref Exception on server side
I have a multiplayer game where no object is instantiated at run-time. They all exist in the scene prior to launch, so each object is the same across the network.
Each player has a script attached that constantly Raycasts and returns the relevant object they are looking at into a public GameObject variable unique to that player's script. The server takes care of player movement, and everything is synced up really beautifully in that regard.
Here's my problem: When I run two instances of the game to test the multiplayer, I can clearly see in the Editor that the raycasting is working great, and every player's public object variable is filled correctly with what they're looking at. But, once I actually try to access that object on the Server to manipulate it, it throws a Null Reference Exception. I've pinpointed it to exactly that issue. It's driving me insane. Every instance of the game knows what the object is, and it's showing up clear as day in the Editor that the Server knows what object is being referred to, but the RPC I'm calling always throws the Exception.
NOTES: No, I am not trying to pass an object through an RPC, I know it's not a valid type. Yes, I am using the old, deprecated RPC calls from Unity networking pre-5. I have been building this game for two years already and I can't afford to completely rewrite the networking with UNET, especially since the rest of the game is working brilliantly and smooth as butter. I could just reference the objects using NetworkViews, and I have proven this works, but I was hoping this Raycasting technique would save me from adding a NetworkView component to every object with which players can interact.
I've been pulling what remains of my hair out over this. Any help is greatly appreciated. Thank you!!!
[SOLUTION]
I feel like an idiot. I was creating the ray for my cast like this:
Ray lookRay = GetComponentInChildren().ScreenPointToRay(Input.mousePosition);
So whenever my mouse would leave the area (like when I switched over to another instance of the game) the object would understandably return null. I changed it to this:
Ray lookRay = GetComponentInChildren().ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
And that did the trick. SMH! Sorry everyone. Hopefully this can help someone else if they make the same mistake.
What's even more confusing and infuriating is I can easily Debug.log() or print() what each player sees, across the board. I just can't do it suddenly when it's called in an RPC on the server. NOTE: The client itself seems to have no difficulty finding the object through an RPC that it itself is calling.
Your answer
![](https://koobas.hobune.stream/wayback/20220612052401im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Server-side game "plugins" 1 Answer
Best Multiplayer Middleware 1 Answer
Coop network functionality 0 Answers
How to access caller of command from from server? 1 Answer
Multiplayer player look at hit.point but other players twitch 0 Answers