Unet Instantiate

I have this simple building script, where if i press q i can build objects, like a foundation. But this doesnt sync over UNET? How would i make this script sync over unet?

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.Networking;
public class BuildingSystem : MonoBehaviour
{
     public List<buildObjects> objects = new List<buildObjects>();
     public buildObjects currentobject;
     private Vector3 currentpos;
     private Vector3 currentrot;
     public Transform currentpreview;
     public Transform cam;
     public RaycastHit hit;
     public LayerMask layer;
     public float offset = 1.0f;
     public float gridSize = 1.0f;
     public bool IsBuilding;
     void Start()
     {
         ChangeCurrentBuilding(0);
         Cursor.visible = false;
         Cursor.lockState = CursorLockMode.Locked;
     }
     void Update()
     {
         if(Input.GetKeyDown("q"))
         {
             IsBuilding = true;
         }
         else if(Input.GetKeyDown("e"))
         {
             IsBuilding = false;
         }
         if (IsBuilding == false)
         {
             currentpreview.gameObject.SetActive(false);
         }else
         {
             currentpreview.gameObject.SetActive(true);
         }
         if (IsBuilding == true)
         {
             startPreview();
         }
         if (Input.GetButtonDown("Fire1"))
         {
             Build();
         }
         if (Input.GetKeyDown("0") || Input.GetKeyDown("1") || Input.GetKeyDown("2"))
         {
             switchCurrentBuilding();
         }
     }
     public void switchCurrentBuilding()
     {
         for (int i  = 0; i < 3; i++)
         {
             if(Input.GetKeyDown("" + i))
             {
                 ChangeCurrentBuilding(i);
             }
         }
     }
     public void ChangeCurrentBuilding(int cur)
     {
         currentobject = objects[cur];
         if(currentpreview != null)
         {
             Destroy(currentpreview.gameObject);
         }
         GameObject curprev = Instantiate(currentobject.preview, currentpos, Quaternion.Euler(currentrot)) as GameObject;
         currentpreview = curprev.transform;
     }
     public void startPreview()
     {
         if (Physics.Raycast(cam.position, cam.forward, out hit, 10, layer))
         {
             if(hit.transform != this.transform)
             {
                 showPreview(hit);
             }
         }
     }
     public void showPreview(RaycastHit hit2)
     {
         currentpos = hit2.point;
         currentpos -= Vector3.one * offset;
         currentpos /= gridSize;
         currentpos = new Vector3(Mathf.Round(currentpos.x), Mathf.Round(currentpos.y), Mathf.Round(currentpos.z));
         currentpos *= gridSize;
         currentpos += Vector3.one * offset;
         currentpreview.position = currentpos;
         if (Input.GetButtonDown("Fire2"))
         {
             currentrot += new Vector3(0,90,0);
         }
         currentpreview.localEulerAngles = currentrot;
     }
     public void Build()
     {
         PreviewObject PO = currentpreview.GetComponent<PreviewObject>();
         if (PO.IsBuildable)
         {
             Instantiate(currentobject.prefab, currentpos, Quaternion.Euler(currentrot));
         }
     }
}
[System.Serializable]
public class buildObjects
{
     public string name;
     public GameObject prefab;
     public GameObject preview;
     public int Gold;
}

Put NetworkIdentity (and NetworkTransform too I think?) scripts on the things you build.

i have, the “previewbuild” shows, but when you Instantiate the item(prefab) it doesnt spawn for the other client

Screenshot - 8c10ad4f5fa90e51fa74f2efb93217bc - Gyazo - Client

Screenshot - 89ebc5de56e4f453b38ecb2bac168ca6 - Gyazo - Host

IF the Client builds something it shows up for them, but not the host

bump?

Instantiate the object on the server
Assign any initial synced values you need to on the server (position, SyncVars, etc.)
Then call NetworkServer.spawn(yourGameObject)

you have to use network.spawn or cmd/rpc commands. I don’t see networking in your script?

Ok now its like this, but now i get this error on the host instead:
UnassignedReferenceException: The variable currentpreview of BuildingSystem has not been assigned.
You probably need to assign the currentpreview variable of the BuildingSystem script in the inspector.

I can still build on the host and such, but i get that error on the host if i try to build on the client

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.Networking;

public class BuildingSystem : NetworkBehaviour
{
    public List<buildObjects> objects = new List<buildObjects>();
    public buildObjects currentobject;
    private Vector3 currentpos;
    private Vector3 currentrot;
    public Transform currentpreview;
    public Transform cam;
    public RaycastHit hit;
    public LayerMask layer;

    public float offset = 1.0f;
    public float gridSize = 1.0f;

    public bool IsBuilding;

    void Start()
    {
        ChangeCurrentBuilding(0);

        Cursor.visible = false;
        Cursor.lockState = CursorLockMode.Locked;
    }

    void Update()
    {
        if(Input.GetKeyDown("q"))
        {
            IsBuilding = true;
        }
        else if(Input.GetKeyDown("e"))
        {
            IsBuilding = false;
        }

        if (IsBuilding == false)
        {
            currentpreview.gameObject.SetActive(false);
        }else
        {
            currentpreview.gameObject.SetActive(true);
        }

        if (IsBuilding == true)
        {
            startPreview();
        }
        if (Input.GetButtonDown("Fire1"))
        {
            CmdBuild();
        }
        if (Input.GetKeyDown("0") || Input.GetKeyDown("1") || Input.GetKeyDown("2"))
        {
            switchCurrentBuilding();
        }
    }

    public void switchCurrentBuilding()
    {
        for (int i  = 0; i < 3; i++)
        {
            if(Input.GetKeyDown("" + i))
            {
                ChangeCurrentBuilding(i);
            }
        }
    }

    public void ChangeCurrentBuilding(int cur)
    {
        currentobject = objects[cur];
        if(currentpreview != null)
        {
            Destroy(currentpreview.gameObject);
        }
        GameObject curprev = Instantiate(currentobject.preview, currentpos, Quaternion.Euler(currentrot)) as GameObject;
        currentpreview = curprev.transform;
    }

    public void startPreview()
    {
        if (Physics.Raycast(cam.position, cam.forward, out hit, 10, layer))
        {
            if(hit.transform != this.transform)
            {
                showPreview(hit);
            }
        }
    }

    public void showPreview(RaycastHit hit2)
    {
        currentpos = hit2.point;
        currentpos -= Vector3.one * offset;
        currentpos /= gridSize;
        currentpos = new Vector3(Mathf.Round(currentpos.x), Mathf.Round(currentpos.y), Mathf.Round(currentpos.z));
        currentpos *= gridSize;
        currentpos += Vector3.one * offset;
        currentpreview.position = currentpos;
        if (Input.GetButtonDown("Fire2"))
        {
            currentrot += new Vector3(0,90,0);
        }
        currentpreview.localEulerAngles = currentrot;
    }

    [Command]
    public void CmdBuild()
    {
        PreviewObject PO = currentpreview.GetComponent<PreviewObject>();
        if (PO.IsBuildable)
        {
            GameObject prefab = (GameObject)Instantiate(currentobject.prefab, currentpos, Quaternion.Euler(currentrot));
            NetworkServer.Spawn(prefab);
        }
    }
}

[System.Serializable]
public class buildObjects
{
    public string name;
    public GameObject prefab;
    public GameObject preview;
    public int Gold;
}