I do have a network Identity script attached to my player prefab. The idea is to spawn the playprefab when clients join, and disable the playerController script on the prefab spawn by other clients.
Short version:
Host side: Host the game on the network, player prefab created because I host and join the game. The prefab can be control normally.
Client side: Join the game hosted above. player prefab created > player controller script destroyed because the client side do not think I am a localplayer
Long version:
When I host the game, this works fine. I can control the player prefab spawned. However on the client side, the player prefab spawned does not seen as a localplayer gameobject. As a result the player controller script for client player prefab is destroyed as I join the game. I cannot control it.
PS: When I use the default NetworkManager and put a default player prefab in it, there is no problem.
I am a total beginner. I am so sorry if I cannot express myself good enough. Thank you for u guys help in advance.
link to the picture of my inspector of my prefab: image hosted at ImgBB — ImgBB
Here is the code for my custom NetworkManager. This spawn a player object when a client is connected to the server.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class GameManager : NetworkManager
{
public GameObject[] playerModels;
public Transform[] playerSpawnPos;
public string role = null;
public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
{
GameObject player = (GameObject)Instantiate(playerModels[1], playerSpawnPos[0].position, Quaternion.identity);
NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
Debug.Log(playerControllerId);
}
}
Here is a part of my player controller script with my player prefab.
void Start()
{
player = GetComponent<CharacterController>();
//fpsCam = GetComponentInParent<Camera>(); //
startPos = transform.position;
if (!isLocalPlayer)
{
Destroy(this);
pigEyes.SetActive(false);
// gameObject.SetActive(false);
Debug.Log("CAMoff");
return;
}
}