Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 $$anonymous$$ · Dec 21, 2014 at 05:45 PM · c#networkingnetworkrpc

Networking RPC sends to wrong target

I am making a card game. The host player or "server" has a Menu Object and _Main1 in their scene. The other player or "client" also has a unique Menu Object but a _Main2 in their scene. As a child for each _Main object there is a Player object and under that a Deck, Hand, Field, Graveyard. E.g. _Main1>Player1>Deck,Hand,Field,Graveyard & _Main2>Player2>Deck,Hand,Field,Graveyard. In both of the _Main objects there are the following integer variables: NoOfCards and NoOfCards2. For the Server NoOfCards is set to the amount of children In the "Hand" object which contains objects named "Cards".

Example setup

So when the game is first setup in _Main1, NoOfCards is set to 5. Now it needs to find the value of NoOfCards2. The way to do this I believe is get the value straight from _Main2 on the "client". To do this I use RPCs. Below is the section of code which tries to find the values of NoOfCards(2), the _Main1 and _Main2 and instantiated from one prefab and so I have the if(host) function to check which section it should run.

 if(host){
             NoOfCards = Player_1.transform.FindChild("Hand").childCount;
             NoOfFieldCards = Player_1.transform.FindChild("Field").childCount;
             networkView.RPC("SendData", RPCMode.OthersBuffered, "NoOfCards2");
             networkView.RPC("SendData", RPCMode.OthersBuffered, "NoOfFieldCards2");
             print ("NoOfCards2: "+NoOfCards2+" NoOfFieldCards2: "+NoOfFieldCards2);(**)
         }
         else{
             NoOfCards2 = Player_2.transform.FindChild("Hand").childCount;
             NoOfFieldCards2 = Player_2.transform.FindChild("Field").childCount;
             networkView.RPC("SendData", RPCMode.Server, "NoOfCards");
             networkView.RPC("SendData", RPCMode.Server, "NoOfFieldCards");
             print ("NoOfCards: "+NoOfCards+" NoOfFieldCards: "+NoOfFieldCards);
         }

Here are the RPCs that are used in the code above. It is designed so that you call for a variable from another object on the "client" for example using a string for the variable name. It then finds that variable on the "client" and returns it using reflection.

 [RPC]
     void SendData(string DataName){
         print (this.GetType().GetField("NoOfCards2"));(*)
         print (host);
         if(this.GetType().GetField(DataName).GetValue(this).GetType() == typeof(int)){
             networkView.RPC("ReceiveDataInt",RPCMode.AllBuffered,DataName,this.GetType().GetField(DataName).GetValue(this).GetType());
         }
     }
 
     [RPC]
     void ReceiveDataInt(string RequestedDataName, int DataReceived){
         this.GetType().GetField(RequestedDataName).SetValue(this, DataReceived);
     }

However from the Server when it tries to print(*) it returns Null and therefore the rest of the code fails too. I get the following error: "NullReferenceException: Object reference not set to an instance of an object Main.SendData (System.String DataName)".

Just in case it's useful I also get the following result from this print(**first section of code): "NoOfCards2: 0 NoOfFieldCards2: 0 UnityEngine.MonoBehaviour:print(Object) Main:OnGUI()"

I believe the problem is my setup overall and to do with networkView.viewIDs on the NetworkView components on the Menu objects and the _Main objects but it is beyond my understanding now and need help.

37052852a3a44571e41aac3fc826528a.png (4.0 kB)
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

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

25 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

Related Questions

Multiplayer Moving Bullet 1 Answer

How To Deal With Lingering Prefabs in Multiplayer Scene ? 0 Answers

RPC and multiplayer networking 2 Answers

How can i send a Player list to every Client 0 Answers

Acess server stuff by the client 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