Question by
TheBaldCacti · Jan 03, 2020 at 12:51 PM ·
networkingmultiplayermultiplayer-networking
Unity Multiplayer Objects Spawned by Server/Client show up on client but objects spawned bu client dont show up on sever/client
Heres the code for it
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class BuildController : NetworkBehaviour
{
public LayerMask ground;
public GameObject MainCamera;
public Camera Camera;
public GameObject Building;
GameObject Buildingmid;
bool spawned = false;
public string ObjectTier;
public string ObjectName;
public string Type_Full_or_Preview;
GameObject BuildingPreviewPrefab;
Transform previewTr;
GameObject previewGO;
private void Start()
{
BuildingPreviewPrefab = Resources.Load<GameObject>(ObjectName + "/" + Type_Full_or_Preview + "/Tier" + ObjectTier + ObjectName);
}
void Update()
{
if (!isLocalPlayer)
{
Camera.enabled = false;
return;
}
if (spawned == false)
{
Ray ray = new Ray(MainCamera.transform.position, MainCamera.transform.rotation * Vector3.forward);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, Mathf.Infinity, ground))
{
previewGO = Instantiate(BuildingPreviewPrefab, hit.point, Quaternion.identity);
NetworkServer.Spawn(previewGO);
spawned = true;
}
}
if (spawned == true) {
Ray ray = new Ray(MainCamera.transform.position, MainCamera.transform.rotation * Vector3.forward);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, Mathf.Infinity, ground))
{
previewGO.transform.position = (hit.point);
}
if (Input.GetButton("Fire1"))
{
Cmdplace(Instantiate(Building, previewGO.transform.position, previewGO.transform.rotation));
placed();
}
if (Input.GetButton("Debug Horizontal"))
{
previewGO.transform.Rotate(0, Input.GetAxisRaw("Debug Horizontal"), 0);
}
}
Debug.Log(previewGO);
}
[Command]
void Cmdplace(GameObject BuildingFinal)
{;
NetworkServer.Spawn(BuildingFinal);
}
void placed()
{
Object.Destroy(this.previewGO);
}
}
Comment