I am a bit confused and would like to ask for some help getting this piece of code to work.
Here is what i try to accomplish:
I have a button that trigger: “OpenMenu”
“anim_Menu.SetBool (“isOut”, true);” trigger an animation that moves the menu into the scene
It does not work!
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
using UnityEngine.UI;
public class UI_Menu : NetworkBehaviour {
public Animator anim_Menu;
public Button btn_OpenMenu;
[SyncVar(hook = "UI_DoMenu")] private bool _openMenu = false;
// Use this for initialization
void Awake () {
anim_Menu.SetBool ("isOut", true);
}
void UI_DoMenu (bool value) {
print ("UI_DoMenu: " + value);
if (_openMenu) {
OpenMenu ();
}
}
[Client]
public void OpenMenu () {
print ("OpenMenu: " + isLocalPlayer);
if (isLocalPlayer) {
anim_Menu.SetBool ("isOut", true);
btn_OpenMenu.interactable = false;
Cmd_OpenMenuProperty (true);
}
}
[Command]
void Cmd_OpenMenuProperty (bool value) {
print ("Cmd_OpenMenuProperty: " + hasAuthority);
if (hasAuthority) {
_openMenu = value;
}
}
}
Somewhat fixed with this code, but it does not work if i click on the client side:
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
using UnityEngine.UI;
public class UI_Menu : NetworkBehaviour {
public Animator anim_Menu;
public Button btn_OpenMenu;
[SyncVar(hook = "SV_DoMenu")] private bool _openMenu = false;
// Use this for initialization
void Awake () {
SetMenu ();
}
void SetMenu() {
anim_Menu.SetBool ("isOut", true);
}
public void Btn_OpenMenu () {
print ("Btn_OpenMenu: " + isLocalPlayer);
Cmd_OpenMenuProperty (true);
}
[Command]
void Cmd_OpenMenuProperty (bool value) {
print ("Cmd_OpenMenuProperty: " + hasAuthority);
_openMenu = value;
}
public void SV_DoMenu (bool value) {
print ("SV_DoMenu: " + value);
if (value) {
Client_OpenMenu ();
}
}
[Client]
public void Client_OpenMenu () {
print ("Client_OpenMenu");
anim_Menu.SetBool ("isOut", false);
btn_OpenMenu.interactable = true;
Cmd_OpenMenuProperty (true);
}
}
I have the following setup:
[ 20mb image hosting ](http:// 20mb image hosting )
Do i need NetworkIdentity for these UI’s given that i try to trigger it locally. When i add it, things looks weird.
Here is the menu i move, down in the bottom left is the one that trigger the animation.
upload photos
If I am not mistaken you have to assign your _openMenu variable in the hook function. I hope it helps.
public void SV_DoMenu (bool value) {
_openMenu = value; <-- Here
print ("SV_DoMenu: " + value);
if (value) {
Client_OpenMenu ();
}
I get: “Trying to send command for object without authority.” on the
public void Btn_OpenMenu () {
Cmd_OpenMenuProperty (true);
}
I do not really understand how to fix that as the menu itself, IMHO, do not need to be a network object as it is the bool that trigger. The only thing i want to do is to bring down the menu on both sides.
What would be the simplest way of doing that?
If you want to allow client to call a function on the server without needing [Command]s or authority, you need to use Network Messages. You also don’t need Network Behaviours to send Messages
When I get the “Trying to (…) without authority” then I disable this component and I only enable it when it’s loaded OnStartLocalPlayer like this but remember to disable it on the prefab first. Hope this helps!
public override void OnStartLocalPlayer()
{
this.enabled = true;
}