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

Syncing lists using MLAPI

Discussion in 'Netcode for GameObjects' started by Boomboyag, May 19, 2021.

  1. Boomboyag

    Boomboyag

    Joined:
    Oct 13, 2020
    Posts:
    33
    Hi!

    As I’ve been working on my multiplayer game, I’ve run across a problem with lists. For whatever reason, I cannot figure out how to sync them across the the server and client. I’ve looked at the api, but I’m fairly new to c#, so a lot of it felt like technical jargon. Can anyone explain to me how to sync the lists? Any help would be much appreciated. Thank you all in advance.
     
  2. luke-unity

    luke-unity

    Joined:
    Sep 30, 2020
    Posts:
    306
    To sync a list with MLAPI you can use the NetworkList<T> type.
     
  3. Boomboyag

    Boomboyag

    Joined:
    Oct 13, 2020
    Posts:
    33
    Should my script look like this? (If it does, can you explain why the list doesn’t show in the inspector?)


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using MLAPI;
    5. using MLAPI.Messaging;
    6. using MLAPI.NetworkVariable;
    7.  
    8. public class NetworkList<GameObject>
    9. {
    10. }
    11.  
    12. public class Name : NetworkBehaviour
    13. {
    14. public NetworkList<GameObject> List = new NetworkList<GameObject>()
    15. }
     
  4. luke-unity

    luke-unity

    Joined:
    Sep 30, 2020
    Posts:
    306
    The NetworkList does not show in the inspector currently, you would have to set its values by code.
     
  5. Boomboyag

    Boomboyag

    Joined:
    Oct 13, 2020
    Posts:
    33
    So the code above is fine?
     
  6. luke-unity

    luke-unity

    Joined:
    Sep 30, 2020
    Posts:
    306