- Home /
UNet | I'm having trouble assigning authority to game objects
I'm having serious issues assigning authority to units in my RTS prototype using UNet.
The Player Object has a component that sends a command to the server to spawn units and assign authority to the client. However once the units are spawned, even after OnStartAuthority()
is called, every client and the server all have authority over every unit.
The units and player objects all have NetworkIdentity with Local Player Authority Checked in. (However for many hours I've tested it with all combinations of that checked on and off)
The Spawner code on the Player Object is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class SpawnUnits : NetworkBehaviour {
public GameObject[] units;
public override void OnStartServer()
{
if (isLocalPlayer)
Cmd_SpawnUnitsMethod();
}
[Command]
public void Cmd_SpawnUnitsMethod()
{
Vector3 pos = transform.position;
foreach(GameObject obj in units)
{
GameObject inst = Instantiate(obj, pos, Quaternion.identity);
Debug.Log(connectionToClient);
NetworkServer.SpawnWithClientAuthority(inst,connectionToClient);
pos += Vector3.right * 2;
}
}
}
And the code on the units that make changes depending on their authority is as follows:
public class NetworkedUnit : NetworkBehaviour {
public override void OnStartAuthority()
{
Set();
}
public void Set()
{
if (!hasAuthority)
{
Debug.Log("Turning off non-authority stuff");
GetComponent<UnitBase>().enabled = false;
GetComponent<NavMeshAgent>().enabled = false;
disabled = true;
}
}
And what I've found is that no Unit ever has hasAuthority==false
ever. I've scoured Google for many hours to see what I'm doing wrong, and I've tried many variations of the code including using AssignClientAuthority(Networking.NetworkConnection conn);
Help would be much appreciated! Thanks!
Your answer
Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
UNET Client Cant Send Server Commands, Host Can. 1 Answer
Commands and ClientRPC are not running when called 0 Answers
Problems with Scene Change networking Unet 0 Answers
send data from client to local server 0 Answers