I have a local object, UI menu, on my scene that does not have a NetworkIdentity property. I want to activate this menu locally by using SyncVar-Hook and by that activate the menu. This function is currently in it’s own script.
My question is were should this script reside to work properly? Currently i have it in an empty GameObject with NetworkIdentity but not as a non-player prefab. I get the “Trying to send command for object without authority” warning when running from the client so i suspect it is the location of the script that is the problem.
Is your menu something that the server or other players need to be told about? If it isn’t, that can just be purely local and you don’t need to put any SyncVars or anything in it.
If it’s something like an equip menu where you’re changing stats on your character or other details that other players or the server needs to know about, you can get that sent by going through that player’s player object.
My idea is to use the SyncVar Hook, using a bool, to locally trigger the menu activation, or actually animating it down on the scene, to avoid having the menu being networked. I want all the player to see if someone activates the menu.
I have a “ScriptContainer” in an empty GameObject, with NetworkIdentity (LocalPlayer Authority) attached that host the script.
However, as i am testing to try to get this to work i have limited the script, do not use the hook as I know it works, to just do a [command] print to get it to work. I tried recommendations from @Chom1czek but without success. This is why i guess it is the location of the script that is the problem. I do not want to place the script on the player nor the non-Player objects that i currently have. Maybe create an additional non-player object, tried that.
The “Trying to send command for object without authority” triggers on line 38. isLocalPlayer is false.
With this test i use the Peek GameObject just to change the color to see that i have a hit both on the server and the client. So what i am trying to achieve now is to fire of the [command] from this script but that does not work.
Here is the test script:
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
using UnityEngine.UI;
public class UI_Menu1 : NetworkBehaviour {
// public Animator anim_Menu;
public Button btn_OpenMenu;
private bool yourBool = false;
public GameObject peek;
void Start() {
// Cmd_Print ();
}
/* [SyncVar(hook = "UI_DoMenu")] private bool _myBool = false;
[Client]
void UI_DoMenu(bool value) {
print ("UI_DoMenu");
if (!isLocalPlayer)
return;
print ("!isLocalPlayer");
Cmd_Print ();
}
*/
public void Btn_Button () {
Cmd_Print ("Btn_Button");
string gg = "isLocalPlayer: " + isLocalPlayer;
Cmd_Print (gg);
if (!isLocalPlayer)
return;
peek.GetComponent<SpriteRenderer> ().material.color = Color.yellow;
Cmd_Print ("Passed");
Cmd_Print ("HIT");
/*
if (yourBool) {
yourBool = false;
} else {
yourBool = true;
}
_myBool = yourBool;*/
}
[Command]
void Cmd_Print(string myStr) {
print (myStr);
}
}