Search Unity

GameObject null when passed to command on client?

Discussion in 'Multiplayer' started by IAMBATMAN, Aug 26, 2016.

  1. IAMBATMAN

    IAMBATMAN

    Joined:
    Aug 14, 2015
    Posts:
    272
    I have this code

    Code (CSharp):
    1. // Use this for initialization
    2.     void Start ()
    3.     {
    4.         if(!isLocalPlayer)
    5.         {
    6.             this.enabled = false;
    7.         }
    8.     }
    9.     // Update is called once per frame
    10.     void Update ()
    11.     {
    12.         if(Input.GetKeyDown(KeyCode.F))
    13.         {
    14.             RaycastHit HIT;
    15.             if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out HIT, 7,mask))
    16.             {
    17.                 if (HIT.collider.tag == "Item")
    18.                 {
    19.                     if (HIT.transform.gameObject.GetComponent<Item>() != null)
    20.                     {
    21.                        
    22.                         CmdPickUpObject(HIT.transform.gameObject.GetComponent<Item>().item_id,pickUpPos.gameObject);
    23.                         Destroy(HIT.collider.gameObject);
    24.  
    25.                     }
    26.  
    27.                 }
    28.             }
    29.         }
    30.     }
    31.     [Command]
    32.     void CmdPickUpObject(int objectId, GameObject pickUpPos_)
    33.     {
    34.         if (pickUpPos_ == null)
    35.         {
    36.             print("Pick Up IS null");
    37.         }
    38.         print(pickUpPos_.transform.name);
    39.         GameObject newSpawnedObject = Instantiate(ItemManager.instance.items[objectId],Vector3.zero,Quaternion.identity)as GameObject;
    40.         newSpawnedObject.transform.SetParent(pickUpPos_.transform);
    41.         newSpawnedObject.transform.localPosition = newSpawnedObject.GetComponent<EquippedItem>().placementPOS;
    42.         newSpawnedObject.GetComponent<EquippedItem>().isEqupped = true;
    43.         NetworkServer.Spawn(newSpawnedObject);
    44.         newSpawnedObject.GetComponent<EquippedItem>().parentId = GetComponent<NetworkIdentity>().netId;
    45.     }
    Which works fine when I pick up an object as host, but when I pick up an object as client, the gamobject pickUpPos, from the command CmdPickUpObject is null.

    Please tell me why?

    P.S the script is disabled on all clients except the local player.
     
  2. Xaon

    Xaon

    Joined:
    Feb 28, 2013
    Posts:
    62
    As far as I know the networking isn't able to serialize and pass object references. Which makes sense as references are pointers to a place in local memory space (the object can be stored in different memory block or even doesn't exist on other machines).

    I would recomend adding a int UID variable with SyncVar attribute. Set UID value on the server in a way that each UID will be unique (SyncVar attibute will send the value to the clients). Simply incrementing UIDs should work ok.
    Then when you pick up the Item you can send UID instead of reference. Each client will have to iterate through all Items to find the correct one but that shouldn't be a problem if you keep them in a List.

    Probably using netId and NetworkServer.FindLocalObject / ClientScene.FindLocalObject as suggested here http://forum.unity3d.com/threads/spawn-object-as-child-unet.427813/ will work too and might be simpler then my suggestion.
    I haven't tried it though.
     
    Last edited: Aug 29, 2016
  3. TheMaskedKasper

    TheMaskedKasper

    Joined:
    Apr 4, 2022
    Posts:
    1
    did you manage perhaps to find an answer to your problem ?