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 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) This 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 something 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 this 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
Add comment
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
0

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 things fundamentally wrong. So I try to explain the stuff I try to achieve 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. This 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 this method for now
         }
 
         Invoke("DebugPlayerData", .5f);
     }
 }


When this 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 = this;
 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
Add comment · 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 TargetReceiveBuyData$$anonymous$$enuData(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 ins$$anonymous$$d 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<$$anonymous$$asterList>();
      string moviesString = masterList.GetRandomDataIdsForRpcCall(maxItemsIn$$anonymous$$enu);
      TargetReceiveBuyData$$anonymous$$enuData(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.

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

Unity networking tutorial? 6 Answers

Networking-multplayer issue 1 Answer

[Mirror Networking] NullReferenceException on player when trying to start server a second time 1 Answer

How can I use a GameObject to hold all references in a network game? 0 Answers

Help with multiplayer chat functionality, null reference exception using networkmanager 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