Search Unity

Sending Objects by RPC calls

Discussion in 'Multiplayer' started by Muszekek, Aug 25, 2014.

  1. Muszekek

    Muszekek

    Joined:
    Jan 26, 2014
    Posts:
    20
    Hi guys i have to send GameObject to clients, i tried by RPC calls but it doesn't work
    i was looking for solution in web, but people there say it is impossible to send gameobject or transform by RPC call, so how can i do that in other way???
     
  2. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    Either you serialize it into something that is supported or you send the data needed in a supported format to create the GameObject. GameObject itself is not a valid RPC param.
     
    Muszekek likes this.
  3. Muszekek

    Muszekek

    Joined:
    Jan 26, 2014
    Posts:
    20
    I just have to tell scripts which game object is current target (only server knows that so i have to send this information to clients) is it possible??
     
  4. Beennn

    Beennn

    Joined:
    Sep 11, 2010
    Posts:
    373
    If the GameObject you want to send has a networkview component attached to it, you could send the viewID of the networkview instead. You can then access the GameObject via NetworkView.Find(viewID).gameObject.
     
    Muszekek likes this.
  5. Muszekek

    Muszekek

    Joined:
    Jan 26, 2014
    Posts:
    20
    Ok, but how to do that by rpc call, i tried something like this :
    networkView.RPC("Enemy",RPCMode.All, closest.networkViewID);
    but it doesn't work, any ideas??
     
  6. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    What doesn't work? The RPC or finding the object?
    If you want help, you will need to provide more detailed info.
     
    Muszekek likes this.
  7. Muszekek

    Muszekek

    Joined:
    Jan 26, 2014
    Posts:
    20
    Ok, so i try to send RPC and i am doing this like that but it doesn't work in that way, what is wrong????




    Code (JavaScript):
    1. networkView.RPC("Enemy",RPCMode.Others, closest.networkViewID);
    2.  
    3. var ID : NetworkViewID;
    4.  
    5.  
    6. @RPC
    7. function Enemy (target : NetworkViewID)
    8. {
    9. ID = target ;
    10. }
     
    Last edited: Aug 27, 2014
  8. Beennn

    Beennn

    Joined:
    Sep 11, 2010
    Posts:
    373
    You need to mark the Enemy function as an RPC; put @RPC above it.
     
    Muszekek likes this.
  9. Muszekek

    Muszekek

    Joined:
    Jan 26, 2014
    Posts:
    20
    Ok, now i have @RPC, but i got this error :
    (line 84 is RPC call)


    Sending RPC failed because 'Enemy' parameter 0 was null
    UnityEngine.NetworkView:RPC(String, RPCMode, Object[])
    AI_ZOMBIE1:FindClosestEnemy() (at Assets/Skrypty/AI_ZOMBIE1.js:84)
    AI_ZOMBIE1:Update() (at Assets/Skrypty/AI_ZOMBIE1.js:19)
     
    Last edited: Aug 27, 2014
  10. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    The error is clear: Sending RPC failed because 'Enemy' parameter 0 was null
    The param your sending (closest.networkViewID) is null.
     
    Muszekek likes this.
  11. Muszekek

    Muszekek

    Joined:
    Jan 26, 2014
    Posts:
    20
    Ok, so i changed closest.networkViewID to closest.networkView.viewID, and also i had some stupid mistake
    in the rest of script, so now it is working perfectly , thank you guys for your help : )
     
    Last edited: Aug 27, 2014