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 cesomark · Feb 13, 2017 at 05:10 PM · c#unity 5client-server

(Unet) Issue with Sending a TargetRPC

Hello,

i have a problem sending data from my Server to Clients. This should happen: On Start the Server generates positions and stores them in a position storage. After this happened, the Server generates Cubes at the generated positions.

When a client connects to the Server, it is supposed to fetch the position Info and also generate Cubes at this position. However the data transferred from my server to my client by a targetRPC is null, and i cannot understand what i am doing wrong.

This is the code:

     private PositionStorage positionStorage;
     private int[,] positionInfo;
 
     void Start()
     {
         positionStorage = GameObject.Find("PositionContainer").GetComponent<PositionStorage>();
         if (isServer)
         {
             positionStorage.GeneratePositions();
             positionInfo = positionStorage.GetPositionInfo();
             positionStorage.GenerateCubesAtPosition(positionInfo);
 
         }
         else
         {
             CmdGetPositionInfo();
         }
     }
 
     [Command]
     void CmdGetPositionInfo()
     {
         TargetSendPositionToClient(connectionToClient, positionInfo);
     }
 
 
 
     [TargetRpc]
     public void TargetSendPositionToClient(NetworkConnection target, int[,] info)
     {
         positionStorage.GenerateCubesAtPosition(info);
     }

Am i doing a fundemental mistake somewhere? Thanks for your help!

Comment
Add comment · Show 8
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 Chikari · Feb 13, 2017 at 05:19 PM 0
Share

I believe TargetRPC only accepts certain parameters, e.g. "array of basic types". Your int[,] however, is an array of integer arrays, which is not a basic type. Thus, the reference is lost on the client and it becomes null.

avatar image cesomark Chikari · Feb 13, 2017 at 05:21 PM 0
Share

Thanks for your answer. Do you know how i can send information from array over the network?

avatar image Chikari cesomark · Feb 13, 2017 at 05:36 PM 0
Share

Well, you could flatten your 2D-Array into a 1D-Array, if all your arrays-within-the-array are of the same length. If you pass the length of the sub arrays, then you can rebuild a 2D array. Something like this (not tested):

 int[] flatArray (int[,] input)
 {
     int[] flatArray = new int[input.GetLength (0) * input.GetLength (1)]; //likely IndexOutOfArrayException if array length differs
     for (int i = 0; i < input.GetLength (0); i++) {
         for (int j = 0; j < input.GetLength (1); j++) {
             flatArray [i * input.GetLength (0) + j] = input [i, j];
         }
     }
     return flatArray;
 }

 int[,] to2DArray(int[] input, int subArrayLength){
     int[,] inflateArray = new int[input.Length / subArrayLength, subArrayLength];
     for (int i = 0; i < inflateArray.GetLength (0); i++) {
         for (int j = 0; j < inflateArray.GetLength (1); j++) {
             inflateArray [i,j] = input [i * inflateArray.GetLength (0) + j];
         }
     }
 }
Show more comments
avatar image Chikari · Feb 13, 2017 at 06:40 PM 0
Share

I am guessing that you have that object A in which you assign positionInfo (only if isServer). If you have a host and a client, you have three copies of A. Now the positionInfo has been set on object A on the server. A_server has that info.

Now you are sending a Command (most likely from a player object, as that's what commands are for). That command is executed on the server but in the client object on the server. That object does not have the positionInfo - remember, you only set that on the serverObject, not in the playerObject on the server. ;) That's why positionInfo is now null.

If you reveal more of your code, maybe we can figure something out.

avatar image cesomark Chikari · Feb 13, 2017 at 07:01 PM 0
Share

Thanks for that explanation, i understand the issue now. There is not more code pretty much. Ins$$anonymous$$d of generating an array i just tried it with an array filled with ones, i recieve zeros.

Is there a way to get the "correct" server object? I mean the client object that sits on the server

Here is an example of what does not work:

   private int[] testArray = new int[2];
 
   void Start(){
       if(isServer){
            testArray[0] = 1;
            testArray[1] = 1;
       }
       else {
       CmdTestCommand();
   }
 
    [Command]
    void CmdTestCommand(){
         Debug.Log(testArray[0] + " " + testArray[1]);
    }
avatar image Chikari cesomark · Feb 13, 2017 at 07:07 PM 0
Share

What kind of data does PositionStorage store?

$$anonymous$$aybe you can convert PositionStorage in a struct ins$$anonymous$$d of a class. Then, you can use a SyncListStruct. You can add to it on the server and it automatically syncs to the client. https://docs.unity3d.com/ScriptReference/Networking.SyncListStruct_1.html

Edit: It might also work to make a SyncListStruct of int array. Then, you can reuse your 2D-Array [,] info by putting every sub-array into the list.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Chikari · Feb 13, 2017 at 07:20 PM

This might be a way to sync your info to the client:

 private PositionStorage positionStorage;
 private int[,] positionInfo;
 private SyncListStruct<int[]> positionList;


 void Start()
 {
     positionList = new SyncListStruct<int[]> ();
     positionList.Callback = processPositions;
     positionStorage = GameObject.Find("PositionContainer").GetComponent<PositionStorage>();
     if (isServer)
     {
         positionStorage.GeneratePositions();
         positionInfo = positionStorage.GetPositionInfo();
         positionStorage.GenerateCubesAtPosition(positionInfo);
         for (int i = 0; i < positionInfo.GetLength (0); i++) {
             positionList.Add (positionInfo [i]);
         }

     }
 }

 void processPositions(SyncListStruct<int[]>.Operation op, int item)
 {
     //this is called on the client when positionList updates
 }
Comment
Add comment · Show 1 · 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 cesomark · Feb 13, 2017 at 07:37 PM 0
Share

Thanks for that but thats sadly not what i need for my issue i think.

I have managed to isolate my problem now. This is the simplest code i could write that still does not work:

 using UnityEngine;
 using UnityEngine.Networking;
 
 public class TestClass : NetworkBehaviour {
     int x;
 
     void Start()
     {
         if (isServer)
         {
             x = 1;
             Debug.Log("x value: " + x); // x = 1
         }
         else
         {
             CmdTestCommand();
         }
     }
 
     [Command]
     void CmdTestCommand()
     {
         Debug.Log("x value: " + x);  // x = 0 
     }
 }

Is there no way to access the correct object when using [Command]?

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

330 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

ClientRpc Function not being carried out on clients in Unity3d c# 0 Answers

Random Generation (Dungeon) with improvements in mind. 0 Answers

How to use the results of a dice roll? 2 Answers

OnGUI will not show up? 1 Answer

My audio doesn't play, I'm through my options... 1 Answer


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