• 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
Question by Vencarii · Mar 04, 2019 at 04:07 PM · networkingnullreferenceexception

UNet: Server only non-player GameObject NullReferenceException on client

Hello,

I try to setup a network game. In my Player object I use Commands and TargetRPCs to get some data from a GameObject with server authority. (MasterList) T$$anonymous$$s works fine:

 public override void OnStartLocalPlayer()
 {
     CmdGetRandomData();
 }
 
 [Command]
 void CmdGetRandomData()
 {
     masterList = FindObjectOfType<MasterList>();
     masterList.LoadDataAtGameStart();
 
     string str = masterList.GetRandomDataIdsForRpcCall(startCount);
     TargetReceiveRandomData(connectionToClient, str);
 }
 
 [TargetRpc]
 void TargetReceiveRandomData(NetworkConnection target, string str)
 {       
     string[] dataIds = str.Split(';');
 
     foreach (string id in dataIds) {
         CmdGetDataById(int.Parse(id));
     }
 }

But when I try somet$$anonymous$$ng similar in another GameObject that isn't a Player, I get a NullReferenceException. I added the GameObject to the NetworkManager and spawned it in the network, so I can see the object on the client instance. The GameObject has no checkbox set in the NetworkIdentity component.

I use t$$anonymous$$s code:

 void Start()
 {
     Invoke("CmdGetDataForToday", 2);
 }
 
 [Command]
 void CmdGetDataForToday()
 {
     masterList = FindObjectOfType<MasterList>();
     string str = masterList.GetRandomDataIdsForRpcCall(maxItemsInMenu);
 
     TargetReceiveMenuData(Player.GetComponent<NetworkIdentity>().connectionToClient, str);
 }
 
 [TargetRpc]
 void TargetReceiveBuyMoviesMenuData(NetworkConnection target, string str)
 {
     string[] dataIds = str.Split(';');
 
     foreach (string id in dataIds) {
         CmdGetDataById(int.Parse(id));
     }
 }
 

I get the NullReferenceException at "string str = masterList.GetRandomDataIdsForRpcCall(maxItemsInMenu);". I don't understand that, because the MasterList is server only and called in a Command.

Comment

People who like this

0 Show 0
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

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by Vencarii · Mar 05, 2019 at 06:40 PM

First of all, thanks for your help so far @ConcanoPayno ! :)

But I still don't get it to work, maybe I have a conceptual error and do t$$anonymous$$ngs fundamentally wrong. So I try to explain the stuff I try to ac$$anonymous$$eve as exact as possible, maybe someone has the time and joy to help.

Explanation: I have a GameObject called "MasterList". It has some gameplay data in it that should be handled by the server. The data is shared by all other players and every record must be unique in the game world, so no two players are allowed to have the same record. My initial plan was to spawn the "MasterList" as server only, but now I didn't set any option in Network Identity.

At game start I receive 5 entries from "Master List" for every player. T$$anonymous$$s works fine for all, Host/Client and Standalone Client.

 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Networking;
 using UnityEngine.UI;
 
 public class Player : NetworkBehaviour
 {
     // start settings
     [SerializeField] int startDataCount = 5;
 
     MasterList masterList;
     List<Data> dataList = new List<Data>();
    
     public override void OnStartLocalPlayer()
     {
         gameObject.tag = "LocalPlayer";
         CmdGetRandomStartData();
     }
     
     [Command]
     void CmdGetRandomStartData()
     {
         masterList = FindObjectOfType<MasterList>();
         if (masterList.AllDataList.Count == 0) {
             masterList.LoadDataAtGameStart();
         }
 
         string dataString = masterList.GetRandomDataIdsForRpcCall(startDataCount);
         TargetReceiveStartData(connectionToClient, dataString);
     }
     
     [TargetRpc]
     void TargetReceiveStartData(NetworkConnection target, string data)
     {       
         string[] dataId = data.Split(';');
 
         foreach (string id in dataId) {
             CmdGetDataById(int.Parse(id));    // gets detailed data info back, I spare t$$anonymous$$s method for now
         }
 
         Invoke("DebugPlayerData", .5f);
     }
 }


When t$$anonymous$$s is done I try to receive 10 more records. I want to use these records to fill a UI menu with the data. The GameObject BuySellData has no checkbox set in Network Identity as well.

Player Object:

 // called in the Player object at the end of OnStartLocalPlayer()
 BuySellData buySellData = FindObjectOfType<BuySellData>();
 buySellData.Player = t$$anonymous$$s;
 buySellData.CmdGetBuyDataForToday();


UI Menu (BuySellData):

 [Command]
 public void CmdGetBuyDataForToday()
 {
     masterList = FindObjectOfType<MasterList>();
     string moviesString = masterList.GetRandomDataIdsForRpcCall(maxItemsInMenu);
     TargetReceiveBuyDataMenuData(Player.GetComponent<NetworkIdentity>().connectionToClient, dataString);
 }
 
 [TargetRpc]
 void TargetReceiveBuyDataMenuData(NetworkConnection target, string data)
 {
     string[] dataIds = data.Split(';');
 
     foreach (string id in dataIds) {
         CmdGetDataById(int.Parse(id));    // spared for now again
     }
 }

So you can see that the method are mostly identical. They work in the Player object, but they don't work in BuySellData. I don't get any error message on the client, there's just no data received from "MasterList".

Comment

People who like this

0 Show 5 · 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
avatar image bpaynom · Mar 06, 2019 at 08:45 AM 0
Share

Is BuySellData.Player field synced ? If not, this line TargetReceiveBuyDataMenuData(Player.GetComponent<NetworkIdentity>().connectionToClient, dataString); just get the connectionToClient of the server object, because it's executed on the server. On the other hand, if BuySellData has no authority, you can't do buySellData.CmdGetBuyDataForToday();


You could do instead something like this:

  // called in the Player object at the end of OnStartLocalPlayer()
  BuySellData buySellData = FindObjectOfType<BuySellData>();
  buySellData.Player = this;
  CmdGetBuyDataForToday( buySellData );

And this function to Player

 //This funcion is on the Player script
 [Command]
 public void CmdGetBuyDataForToday ( BuySellData bsd )
 {
     bsd.GetBuyDataForToday( this.connectionToClient );
 }

And this one to BuySellData

 //This one is on the BuySellData script
 public void GetBuyDataForToday( NetworkInstanceId connectionToClient)
 {
      masterList = FindObjectOfType<MasterList>();
      string moviesString = masterList.GetRandomDataIdsForRpcCall(maxItemsInMenu);
      TargetReceiveBuyDataMenuData(connectionToClient, dataString);
 }




avatar image bpaynom bpaynom · Mar 06, 2019 at 02:36 PM 0
Share

Did you get it to work?

avatar image Vencarii bpaynom · Mar 06, 2019 at 06:44 PM 0
Share

I restructured it a little bit differently. I put all the Commands and TargetRpcs in the Player object and got the reference to the BuySellData there, this works. It may not be the best solution, but it works for now.

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

144 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

Related Questions

(Pun2) Hastable. Null Reference Error 0 Answers

Help with multiplayer chat functionality, null reference exception using networkmanager 0 Answers

Null Reference Exception when using if(!islocalplayer) destroy 1 Answer

Unity Networking - Error on spawning Player 0 Answers

Not sure why I keep getting a Null Reference Exception? (Networking) 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