Unet assigning local player Authority to a gameObject.

Updated on July 8, 2017 in [A] Multiplayer
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
2 on July 8, 2017

Hello everyone, i have been working on this issue for a couple days now and i can’t seem to find an answer. So i was hoping one of you inelegant beings could help me out 😉

 

What i want to do is when the player connects to the server i want them to spawn the chosen character they picked in the lobby screen. I have that part done, but the issue is that when i try and spawn the player using NetworkServer.SpawnWithLocalPlayerAuthorty, it seems it does not want to give local auth to me.

 

Here is some of my code. This code is placed on an empty game object that is spawned when a player connects. And yes it has local Player Auth checked using

 

public class SpawnPlayer : NetworkBehaviour {

public GameObject[] playerPrefabs;
public int playerIndex = 0;
float timer = 3;

//Spawn Delay to wait for server.
private void Update()
{
timer -= 1 * Time.deltaTime;
if(timer <= 0)
{
CmdSpawnPlayer(playerIndex);
timer = 1000;
}

}

//Spawns character on the network.
[Command]
public void CmdSpawnPlayer(int playerIndex)
{
GameObject chosenCharacter = playerPrefabs[playerIndex];
GameObject go = Instantiate(chosenCharacter, new Vector3(0, 1, 0), Quaternion.identity) as GameObject;
NetworkServer.SpawnWithClientAuthority(go, connectionToClient);
gameObject.transform.GetChild(0).gameObject.SetActive(false);
}

}

 

And i have another script that’s on the object being instantiated on the network that should sync the components to give the player control over themselves.

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class SyncComponents : NetworkBehaviour {

public MonoBehaviour[] componentsToEnable;
public GameObject enableCam;

private void Start()
{
if (!isLocalPlayer)
return;

enableCam.SetActive(true);

for (int i = 0; i < componentsToEnable.Length; i++)
{
componentsToEnable[i].enabled = true;
}
}

}

 

 

What seems to be happening is that when it goes to enable the components it wont because it does not have LocaPlayerAuth for some reason i cant understand. The object being spawned has LocaPlayerAuth ticked so that shouldn’t be the issue. Any help is greatly appreciated.

 

  • Liked by
Reply
1 on July 8, 2017

Is the network identity component set to local?

on July 8, 2017

Yes, yes it is.

Show more replies
  • Liked by
Reply
Cancel