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

How do I synchronize GameObjects or Components?

Discussion in 'Multiplayer' started by maikklein, Aug 12, 2015.

  1. maikklein

    maikklein

    Joined:
    Jun 16, 2015
    Posts:
    33
    For example in World of Warcraft you can target a player and everyone can see who you are targeting.

    I could write something like this

    Code (CSharp):
    1. Player targetPlayer;
    2. //or
    3. GameObject targetGameObject
    Now when the server has a new target I want the client to also know the target, how do I do this?

    If I do

    Code (CSharp):
    1.  
    2. [SyncVar]
    3. Player targetPlayer;
    4.  
    The editor just crashes.
    *Player is a NetworkBehavior.


    Code (CSharp):
    1.  
    2. [SyncVar]
    3. GameObject targetGameObject;
    4.  
    This will result in targetGameObject to always be null, on the server and on the client.
     
  2. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    GameObjects with Networkidentities can be SyncVars. You could also use a NetworkInstanceId (the NetworkBehaviour.netId).

    There were some issues with this in earlier release, so be sure to use latest editor patch release.
     
  3. Palimon

    Palimon

    Joined:
    Apr 18, 2013
    Posts:
    225
    If you look at the docs for SyncVar (http://docs.unity3d.com/ScriptReference/Networking.SyncVarAttribute.html) there's a tiny point that I missed on my first SyncVar try too: "Only simple values can be marked as [SyncVars]". What this means is that SyncVars have to be primatives; classes aren't supported. You have to build your own syncing for any classes. I can't say if that's different for GameObjects. I'm still working through syncing myself, but here's a great resource I've found: https://www.youtube.com/watch?t=49&v=NLnzlwCRjgc. He specifically covers player syncing very well. Good luck!
     
    SpookySF likes this.
  4. maikklein

    maikklein

    Joined:
    Jun 16, 2015
    Posts:
    33
    That is what I am currently using, NetworkInstanceId + FindLocalObject.

    I am on the latest release 5.1.2p3 and syncvar for GameObjects doesn't work.

    Code (CSharp):
    1. [SyncVar]
    2. public GameObject test;
    3. ...
    4.  
    5. public override void OnStartServer()
    6. {
    7.     test = (GameObject)Instantiate(prefabPlayerCam, prefabPlayerCam.transform.position,
    8.             prefabPlayerCam.transform.rotation);
    9. }
    The variable test will always be null on the server and on the client if I mark it with SyncVar.

    Code (CSharp):
    1.  
    2. public GameObject test;
    3. ...
    4.  
    5. public override void OnStartServer()
    6. {
    7.  
    8.     test = (GameObject)Instantiate(prefabPlayerCam, prefabPlayerCam.transform.position,
    9.             prefabPlayerCam.transform.rotation);
    10. }
    By removing SyncVar from the GameObject, the GameObject is now not null on the server but is null on the client as expected.

    I think this might be a bug? I am on Windows 10 x64 if that has any relevance.

    Edit:

    And yes "prefabPlayercam" has a NetworkIdenty script attached.

    Edit:

    I just forgot to spawn it. It seems to be working now, thanks!
     
    Last edited: Aug 13, 2015