1

Hello I am trying to create my first game in unity which I wanna be network aware but I encoured a problem with network prefabs spawning. 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;
        }
    }

}

Compiler says there's no problem but in unity I've got such error: "UNetWeaver error: Script Builder uses [Command] CmdSpawnBuilding but is not a NetworkBehaviour. UnityEngine.Debug:LogError(Object)" My code runs perfetly good without "[Command]" line despites it's not network aware. Also, I know it's a bit messed up but I was trying to figure it out what's wrong and so yeah, messed code a bit.

Seoner
  • 101
  • 8
  • Possible duplicate of [Unity3d Unet multiplayer - only server host can place buildings network aware](http://stackoverflow.com/questions/40413680/unity3d-unet-multiplayer-only-server-host-can-place-buildings-network-aware) – Seoner Nov 04 '16 at 01:02

1 Answers1

2

Your class needs to inherit from NetworkBehaviour, like this:

public class Builder : NetworkBehaviour 
turnipinrut
  • 614
  • 4
  • 12