Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Wenamun · Dec 13, 2015 at 02:49 PM · multiplayerraycasthitnull reference exceptionserverside

[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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Wenamun · Dec 12, 2015 at 10:19 PM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges