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 /
  • Help Room /
avatar image
0
Question by joshuahaag · Oct 03, 2017 at 03:48 PM · networkingunity5nullreferenceexception

Bizarre NullReferenceException like no other

If anyone has any idea what could be causing a problem like this it would be great. It has completely stumped both me and my coworker. This project is a server authoritative networked game on Unity 5.6.2p2.

After the first player connects the player's client is able to spawn and initialize the player prefab, including the playercontroller, inventory, and UI.

When the second player connects the player prefab is instantiated and the playercontroller's OnStartLocalPlayer executes in which the UI is instantiated and a reference to the UI monobehaviour is assigned. At this point the reference to the UI is valid.

This is where it gets weird. Only on the second player that connects (and every player after the first), the reference (playerUI) to the UI appears to be null every other frame. The UI is never destroyed and it is always visible in the scene hierarchy. The only time that playerUI is being assigned to is in the original OnStartLocalPlayer function.

Creating a reference to the UI in a different script yields the same results. BUT I am able to get a reference to the UI every frame if I search for the monobehaviour type in the scene (using FindObjectOfType) every other frame when it is null.

Sorry if my writing is unclear. I'd be happy to clarify/elaborate further if anyone thinks they may have some insight.

Comment
Add comment · Show 2
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 FortisVenaliter · Oct 03, 2017 at 05:49 PM 0
Share

Put a Debug.Log in your OnStartLocalPlayer function. $$anonymous$$ake absolutely sure it's only being called when it's supposed to.

avatar image joshuahaag FortisVenaliter · Oct 03, 2017 at 05:57 PM 0
Share

Thanks for the response!

This was one of the first things I tested, OnStartLocalPlayer is only called once after the player's initial player prefab instantiation.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by joshuahaag · Oct 03, 2017 at 05:52 PM

I've managed to solve the problem by moving the initialization of the inventory from OnStartClient to a public Init function. So in the playercontroller's OnStartLocalPlayer it looks like this now:

playerUI = Instantiate(prefab_PlayerUI)

inventory.Init()

playerUI.Init(this)

Though I'm not exactly sure how this fixed the problem considering the first player to connect was able to initialize and use everything correctly but at this point I'm not going to question it.

I'd still like to hear anyone's ideas, just out of pure curiosity :)

Comment
Add comment · Share
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

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

157 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Reference lost in NetworkEntity 0 Answers

Photon RPC Parameter NullReferenceException 1 Answer

NullReferenceException when using gameObject.SetActive(false); 0 Answers

How can I fix a null pointer exception? (2d platformer tutorial) 1 Answer

Communication protocol design with Unity 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