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];
}
}