I am trying to make a construction system like in survival games that works in multiplayer. Here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class CreationSystem : NetworkBehaviour {
public static GameObject SelectedObeject;
bool Preview;
GameObject X;
Material normal;
[SerializeField]
Material previewMaterial;
void Update(){
Cmd_Creation ();
}
[Command]
void Cmd_Creation() {
if(Preview == false && MainMenu.GoPreview && SelectedObeject != null){
RaycastHit rh;
if(Physics.Raycast(transform.GetChild(0).position,transform.GetChild(0).forward,out rh,60)){
X = (GameObject)Instantiate (SelectedObeject, rh.point, SelectedObeject.transform.rotation);
normal = X.GetComponent<Renderer> ().material;
X.GetComponent<Renderer> ().material = previewMaterial;
X.GetComponent<Collider> ().enabled = false;
if(X.transform.childCount > 0){
for(int i=0; i<X.transform.childCount;i++){
X.transform.GetChild(i).GetComponent<Renderer> ().material = previewMaterial;
X.transform.GetChild(i).GetComponent<Collider> ().enabled = false;
}
}
NetworkServer.Spawn (X);
Preview = true;
}
}
if(Preview){
RaycastHit rh;
if(Physics.Raycast(transform.GetChild(0).position,transform.GetChild(0).forward,out rh,60)){
X.transform.position = rh.point;
}
if(Input.GetKeyDown(KeyCode.Mouse0)){
MainMenu.GoPreview = false;
X.GetComponent<Renderer> ().material = normal;
X.GetComponent<Collider> ().enabled = true;
Preview = false;
if(X.transform.childCount > 0){
for(int i=0; i<X.transform.childCount;i++){
X.transform.GetChild(i).GetComponent<Renderer> ().material = normal;
X.transform.GetChild(i).GetComponent<Collider> ().enabled = true;
}
}
}
}
}
It was to work in this way: the player select an object in the menu, that way the variable "SelectedObeject" is changed to the selected object. After the object be selected the preview will starts and if the player click the left mouse button the preview will end and the object will be "Created".
The host can construct with no problem. But the client isn't calling the function "Cmd_Creation()".