1

What I want to do · Launch application 1, application 2 · If I press the button in the application 1, the button will be pressed even in the application 2 (change the click event or bool type variable)

What I tried (I'm putting Unity files here) https://drive.google.com/open?id=1aV-1SiB56L3JGVzWqAmqc85PYFNW1268

Add the following to the object of the button, click

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

public class MoviePlay : NetworkBehaviour {
    [SyncVar]
    bool IsPlay = false;

    void Update(){
        if(IsPlay){
            Debug.Log("再生中");
        }        
    }

    public void OnClick(){
        CmdOnClick();
    }
    [Command]
    void CmdOnClick(){
        IsPlay = true;
    }
}

Result: The console displays Did not find target for sync message for 1 and is not synchronized

-----Add------

I tried it. PushButton(Player Object)

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

public class PushButton : NetworkBehaviour {
    [SerializeField]
    GameObject DebugScript;

    public void OnClickButton(){
        NetworkInstanceId target = DebugScript.GetComponent<NetworkIdentity>().netId;
        CmdSendMessageToServer(target, "DebugText");
    }
    [Command]
    public void CmdSendMessageToServer (NetworkInstanceId target, string message)
    {
        NetworkServer.FindLocalObject(target).SendMessage(message);
    }
}

DebugScript

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class DebugScript : MonoBehaviour {
    void DebugText(){
        Debug.Log("再生");
    }
}
  • 1
    Please provide a [MCVE](https://stackoverflow.com/help/mcve) : It does not seem that you need people to download a "unity package" so you should try to do without it. Also using a "tmpfile" directory in a third-party cloud provider will make your question harder to archive. – Gabriel Devillers Aug 06 '18 at 18:12

1 Answers1

0

Commands can ONLY be called on player objects. So if it isn't the local player, commands simply won't be called. You must move the function to the player object, and call a method on the button.

Instead of making tons and tons of methods on the player, I made a single method to just utilize the SendMessage method of every GameObject. I had a method on the player object called CmdSendMessageToServer, which had two parameters, a NetworkInstanceId and a string. It looked a bit like this...

[Command]
public void CmdSendMessageToServer (NetworkInstanceId target, string message)
{
     NetworkServer.FindLocalObject(target).SendMessage(message);
}

Pass 'netId' (the NetworkInstanceId) variable of your button to the above function, along with the name of the method you would like to call (in this case, OnClick).

Hope this helps!

EDIT: For clarification, you should make a separate class that is placed on the player object. Like the following...

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

public class TestNetworkPlayer : NetworkBehaviour 
{
     //a singleton for the CLIENT
     public static TestNetworkPlayer local;

     public override void OnStartLocalPlayer ()
     {
        base.OnStartLocalPlayer();
        //so whenever we access TestNetworkPlayer.local,
        //it will ALWAYS be looking for the LOCAL player on the client we use
        local = this;
     }

    [Command]
    public void CmdSendMessageToServer(NetworkInstanceId target, string message)
    {
        NetworkServer.FindLocalObject(target).SendMessage(message);
    }
}

and then in your 'MoviePlay' script, you would have this code instead...

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

public class MoviePlay : NetworkBehaviour {
    [SyncVar]
    bool IsPlay = false;

    void Update(){
        if(IsPlay){
            Debug.Log("再生中");
        }        
    }

    //called when the button is clicked
    public void OnClick(){
        TestNetworkPlayer.local.CmdSendMessageToServer (netId, "ServerOnClick");
    }

    //ONLY called on the server, and then IsPlay should change on all clients as well
    void ServerOnClick(){
        IsPlay = true;
    }
}

Hope this cleared it up!

Scornz
  • 380
  • 3
  • 14
  • I tried it. However, the remote client did not change. – Tsunehiko Shimadu Aug 06 '18 at 19:26
  • @TsunehikoShimadu Like I said in my post, this method **must** go on the player object. You **cannot* call commands on anything other than player objects. You put this method on your button, hence why it did not work. Also, the 'DebugText' would've never have gotten called on the client anyway, since you were calling it in a command. If you changed the SyncVar however through the method (like in your original example), you would be changing it on the server, and then the SyncVar attribute would automatically send that value to all clients. – Scornz Aug 07 '18 at 12:36
  • @TsunehikoShimadu I edited my answer to better clarify what I was trying to explain. – Scornz Aug 07 '18 at 12:49