Search Unity

Third Party [Mirror] Objects not properly creating across network

Discussion in 'Multiplayer' started by KingCole32, Jan 13, 2020.

  1. KingCole32

    KingCole32

    Joined:
    Aug 15, 2019
    Posts:
    1
    I have a NetworkManager that creates an object on game start as below. This works for the client and host.

    Code (CSharp):
    1.  
    2. puck = Instantiate(spawnPrefabs.Find(prefab => prefab.name == "Puck"));
    3. NetworkServer.Spawn(puck);
    4.  
    The manager also creates the players, each an objects that create objects on player clicks. Players are also created seemingly without issue. I've tried changed this all sorts of ways, but always get variations of the same issue. In the player object, I try creating a new object on click via this:


    Code (CSharp):
    1.  
    2. ....
    3.    if (isLocalPlayer)
    4.    {
    5.  
    6.    Vector2 mousePosition = m_Camera.ScreenToWorldPoint(Input.mousePosition);
    7.    if (Input.GetMouseButtonDown(0))
    8.    {                  
    9.          m_currentSwipe = Instantiate(swipePrefab, Vector2.zero, Quaternion.identity);
    10.          m_swipeList.Add(m_currentSwipe);
    11.          Cmd_CreateNewSwipe();                  
    12.    }
    13. ....
    14.  
    15. [Command]
    16. private void Cmd_CreateNewSwipe()
    17. {
    18.     NetworkServer.Spawn(m_currentSwipe);          
    19. }
    20.  
    This works locally, but isn't creating the object for clients. I feel like I just don't understand where replication is supposed to take place, even after looking at other examples and other questions.

    Any tips would be greatly appreciated. I feel like once I get this one thing down, I'll better understand mirror, in general.
     
    MentalGames and akuno like this.
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You are instantiating on the client then calling a Command and trying to spawn the instantiated object on the server, but the server doesn't know what the hell you're telling it to do since it hasn't yet instantiated the object. You need to both instantiate and spawn on the server only.