Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Resolved Unity Mirror - How to send array of serializable obj (not GameObject) by NetworkMessage

Discussion in 'Scripting' started by LambdaTheDev, Nov 5, 2020.

  1. LambdaTheDev

    LambdaTheDev

    Joined:
    Jan 15, 2020
    Posts:
    53
    OBJECT is unsupported, that's why it's not working.


    Hello guys.
    When developing the game I found a problem with sending data through the connection.Send();

    This is my NetworkMessage template:
    Code (CSharp):
    1. public class DataFetchResult : NetworkMessage
    2. {
    3.     public int fetchId;
    4.     public DataFetchType type;
    5.     public object fetchData;
    6. }
    In some place of DataFetcher, I have the List<> of Badges. After fetching all the data, I execute this function:
    Code (CSharp):
    1.         [Server]
    2.     void ReturnMessage(int id, DataFetchType type, object message, NetworkConnection connection)
    3.     {
    4.         DataFetchResult result = new DataFetchResult
    5.         {
    6.             fetchId = id,
    7.             type = type,
    8.             fetchData = message
    9.         };
    10.  
    11.         connection.Send(result);
    12.     }
    But when I execute the method below in OnReturnMessage function, it returns me System.Object instead of System.Badge[] (BEFORE EXECUTING FUNCTION ABOVE, I CAST LIST TO ARRAY, USING List<T>.ToArray() !).
    Code (CSharp):
    1. result.fetchData.GetType().ToString().
    Does anyone know what can I do?
     
    Last edited: Nov 5, 2020
  2. keola117

    keola117

    Joined:
    Sep 11, 2021
    Posts:
    15
    ever figure out how to get this to work?
     
  3. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    804
    Well fetchData was declared as an object type. So it makes total sense that GetType returns System.Object. @keola117 you will need to provide code and probably creat your own thread, but you might need to convert from object to whatever type you used.
     
  4. keola117

    keola117

    Joined:
    Sep 11, 2021
    Posts:
    15
    yeah, i fixed it by converting complicated data into structs and then representing the segments into more simple chunks, example, PlayerResource(string name, enum type, int ammount){ if (type == enum.type){ stringenumToType = enum.type.ToString; }} then I pass the datatype through a playFab list contructor using 128bytestring and a intbyte. Yeah, I figured it out by myself but, even so, i like to just try googling things asap so i don't have to think hard. haha