0

I am writting an multiplayer game but I am stuck with building system - I mean I've created a code to place blocks it works perfectly fine on host-side (when you run this script on host the block spawn for every client) but it isn't working on client side ( blocks spawn only for local client but not for server and other clients ). I've seen many simillar problems but no answer was found this day. Maybe you guys could give me a hand. Here's my code:

using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class Builder : NetworkBehaviour {

    public GameObject preview;
    public Transform currentPreview;
    bool isPreviewing = false;
    GameObject buildingPreview;
    private NetworkIdentity networkId;

    // Use this for initialization
    void Start ()
    {
        networkId = GetComponent<NetworkIdentity>();
    }

    // Update is called once per frame

    void ViewPreview()
    {
        buildingPreview = Instantiate(preview, transform.position, transform.rotation) as GameObject;
        currentPreview = buildingPreview.transform;
        isPreviewing = true;
    }
    void Update ()
    {
        CmdBuild(); 
    }

    void CmdBuild()
    {
        if (networkId.isLocalPlayer)
        {

        }
        else
        { return; }
        if (Input.GetKeyDown(KeyCode.E))
        {
            if (!isPreviewing)
                ViewPreview();
            else
            {
                Destroy(buildingPreview);
                isPreviewing = false;
            }
        }
        if (isPreviewing)
        {
            Preview();
        }
    }


    [Command]
    void CmdSpawnBuilding()
    {
        GameObject buildingPlaced = Instantiate(preview, currentPreview.position, currentPreview.rotation) as GameObject;
        NetworkServer.Spawn(buildingPlaced);
    }

    void Preview()
    {
        currentPreview.position = transform.position + transform.forward * 3f;
        currentPreview.rotation = transform.rotation;
        if (Input.GetButtonDown("Fire1"))
        {
            CmdSpawnBuilding();
            isPreviewing = false;
        }
    }
}

P.S: I have network identity script assigned to prefab I am creating network aware and I have that prefab in my Network Manager as Registred Spawnable Prefabs.

hveiga
  • 6,725
  • 7
  • 54
  • 78
Seoner
  • 101
  • 8
  • Are you missing the [Command] tag on CmdBuild? – Umut Seven Nov 04 '16 at 09:03
  • I was messing around with code a bit and forgot to change it into "Build()" anyways should I put "[Command]" here eventho it's just code for local client? The code that I want to run on my server is inside "CmdSpawnBuilding()" – Seoner Nov 04 '16 at 09:17
  • Should CmdBuild() be called on the server only? if so, you need to add [Command] before the function; if not, the function name shouldn't start with Cmd (wont affect the functionality, but is similar to creating a StartServer function that don't start the server but do something else... documentation and guidelines matter) . – Magrones Jul 04 '17 at 19:08

0 Answers0