1

I'm trying to make 2D-multiplayer game with randomly generated map.

Here's some simplified code what I have tried

public class Map : NetworkBehaviour
{
    private TilePainter _tilePainter;

    [Command]
    public void CmdStartGenerating()
    {        
        _tilePainter.PaintSection(section)//section is randomly generated map information
    }
}
public class TilePainter : NetworkBehaviour
{
    public Tile tile;    

    private Tilemap _tilemap;

    public void PaintSection(Section section)
    {
        section.Foreach(x=>{
            _tilemap.SetTile(x.Vector3(), tile);
        })
    }
}

Of course I made them have NetworkIdentity and CmdStartGenerating is called by button click.

The problem is that generated tilemap is visible only in host player.

The other client cannot see generated tile map.

In real code, TilePainter also instantiate some prefabs and they are visible to both player.

I have tried making them network spawnable prefabs but didn't work. (with same problem)

Is there any method like NetworkServer.Spawn for tilemap??

I'm new to Unity multiplayer feature so maybe I'm making some stupid mistake...

Thanks for reading my question!

####Edit

Uploading additional code I've tested in test scene.

public class NetTest : NetworkBehaviour
{
    public TileRiverMap tileRiverMap;
    
    private SectionGenerator _sectionGenerator =
        new SectionGenerator(10, 20, 1f);
    
    public void Test()
    {
        Debug.Log("hihi");
        tileRiverMap.PaintSection(_sectionGenerator.GenerateSection((0, 0)), 5);
    }
}

Class below is Player object

public class TestPlayer : NetworkBehaviour
{
    private void FixedUpdate()
    {
        if (isLocalPlayer)
        {
            if (Input.GetButtonDown("Jump"))
            {
                CmdTest();
            }
        }
    }

    [Command]
    private void CmdTest()
    {
        FindObjectOfType<NetTest>().Test();
    }
}
public class TileRiverMap : NetworkBehaviour, IRiverMap
{
    public Tile tile;
    public GameObject stone;

    private Tilemap _tilemap;
    ......
}

I don't think whole code of TileRiverMap is needed

shklaurant
  • 45
  • 6
  • 1
    Are you using Unet ,right? Is your Map class a "player object"? https://docs.unity3d.com/Manual/UNetPlayers.html – Lotan Aug 01 '20 at 09:05
  • @Lotan Nope.... So it seems like every '[Command]' should be inside "player object"?. I will try it. thanks :) But it's really weird that prefabs are visible then. – shklaurant Aug 01 '20 at 09:16
  • @Lotan Still facing the same problem. I made "player object" command the Map to start generating. Making Map itself a "player object" didn't solved a problem too. – shklaurant Aug 01 '20 at 12:04

0 Answers0