Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Third Party Mirror - sending game objects from client to server.

Discussion in 'Multiplayer' started by cerestorm, Jul 11, 2021.

  1. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    656
    Is it actually possible?

    I have no problem sending individual values back to the server in commands but I can't package them up into a game object (with network identity) and then send pass that as a parameter into a command.

    Is it possible to send objects back to the server in this way?
     
  2. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    656
    I've still had no joy with this, but looking at the example from the documentation it should work:
    Code (CSharp):
    1. class Item
    2. {
    3.     public string name;
    4. }
    5.  
    6. class Weapon : Item
    7. {
    8.     public int hitPoints;
    9. }
    10.  
    11. class Armor : Item
    12. {
    13.     public int hitPoints;
    14.     public int level;
    15. }
    16.  
    17. class Player : NetworkBehaviour
    18. {
    19.     [Command]
    20.     void CmdEquip(Item item)
    21.     {
    22.         // IMPORTANT: this does not work. Mirror will pass you an object of type item
    23.         // even if you pass a weapon or an armor.
    24.         if (item is Weapon weapon)
    25.         {
    26.             // The item is a weapon,
    27.             // maybe you need to equip it in the hand
    28.         }
    29.         else if (item is Armor armor)
    30.         {
    31.             // you might want to equip armor in the body
    32.         }
    33.     }
    34.  
    35.     [Command]
    36.     void CmdEquipArmor(Armor armor)
    37.     {
    38.         // IMPORTANT: this does not work either, you will receive an armor, but
    39.         // the armor will not have a valid Item.name, even if you passed an armor with name
    40.     }
    41. }
    When I try something similar instead of the object I'm passing I get a copy of the object on the server with its own values rather than the one passed.

    Any thoughts on this?
     
  3. Consumedgrub2

    Consumedgrub2

    Joined:
    Mar 6, 2021
    Posts:
    8
    I have the same issue, I hope by commenting, this brings some attention to this.
     
  4. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    656
    If memory serves the class fields needed to be public.
     
  5. JamesFrowenDev

    JamesFrowenDev

    Joined:
    Oct 10, 2015
    Posts:
    20