-4

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()".

1 Answer 1

0

Commands are invoked on client but executed on server. If you need the server to communicate to client whatever happened due to previous command then you could use Remote Procedure Call or RPC.

// invoked on client but executed on server
[Command]
private void CmdMakeServerSayHelloToClient()
{
    RpcSayHelloToClient(); // invoked on server but executed on client
}

[ClientRpc]
private void RpcSayHelloToClient()
{
    Debug.Log("Hello from server."); // logged on client but not on server
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.