- Home /
Adding Client Authority to non spawned objects?
Hi there, I have bunch of ammos in my editor scene that inherite NetworkBehaviours , also localplayerauthority check boxes are marked .I don't spawn them in runtime ( i active or deactive them for better performance).I want to call a command from them but their islocalplayer's are always false. I cant do
GetComponent<NetworkIdentity>().AssignClientAuthority(GetComponent<NetworkIdentity>().connectionToClient);
too because it can't execute on client. my code is here:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class PlanetOrbitTime : NetworkBehaviour
{
[SerializeField]
private Sprite[] orbitSprites;
private SpriteRenderer sr;
int currentIndex;
bool firstInitialize = false;
private void Awake()
{
orbitSprites = GameReference.Instance.orbitSprites;
}
private void Start()
{
sr = GetComponent<SpriteRenderer>();
}
private void Update()
{
if((isServer || isClient) && !firstInitialize)
{
Debug.Log("islocalPlayer" + isLocalPlayer);
if(isServer)
GetComponent<NetworkIdentity>().AssignClientAuthority(GetComponent<NetworkIdentity>().connectionToClient);
SetSprite(currentIndex);
firstInitialize = true;
}
}
public void Deactive()
{
if (isServer)
RpcDeactive();
else
{
Debug.Log("islocalPlayer" + isLocalPlayer);
gameObject.SetActive(false);
CmdDeactive();
}
}
[Command]
void CmdDeactive()
{
gameObject.SetActive(false);
RpcDeactive();
}
[ClientRpc]
void RpcDeactive()
{
gameObject.SetActive(false);
}
public void SetSprite(int index)
{
currentIndex = index;
if (isServer || isClient)
{
if (isServer)
RpcSetSprite(currentIndex);
else
{
sr.sprite = orbitSprites[currentIndex];
CmdSetSprite(currentIndex);
}
}
}
[Command]
void CmdSetSprite(int index)
{
Debug.Log("cmd" + index);
sr.sprite = orbitSprites[index];
RpcSetSprite(index);
}
[ClientRpc]
void RpcSetSprite(int index)
{
Debug.Log("cmd" + index);
sr.sprite = orbitSprites[index];
}
}
Answer by ojelibalon · Sep 07, 2018 at 03:19 PM
You can assign the authority from the server, so you could have a command method from the player and assign it
thanks for your replay. I $$anonymous$$now i can do this , but it's very dirty coding .and my player has about 1000 lines of code in this way.is there a more smooth way to do this directly from Npc's?
Your answer
Follow this Question
Related Questions
How to setup a lan system 1 Answer
Master Server alternative for iOS basic LAN multiplayer? 1 Answer
I am creating a Co-op, puzzle game, multiplayer app. Lan help needed. 0 Answers
New Input Manager Split Screen and Cinemachine 0 Answers
Trying to Connect a Remote Computer from a Different ISP 0 Answers