Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Client side Player prefab spawned by overriding GameManager return false for isLocalPlayer

Discussion in 'Multiplayer' started by marcussss, Mar 16, 2017.

  1. marcussss

    marcussss

    Joined:
    Mar 16, 2017
    Posts:
    4
    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: https://ibb.co/kswFka


    Here is the code for my custom NetworkManager. This spawn a player object when a client is connected to the server.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5.  
    6. public class GameManager : NetworkManager
    7. {
    8.  
    9.     public GameObject[] playerModels;
    10.     public Transform[] playerSpawnPos;
    11.  
    12.     public string role = null;
    13.  
    14.     public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
    15.     {
    16.  
    17.         GameObject player = (GameObject)Instantiate(playerModels[1], playerSpawnPos[0].position, Quaternion.identity);
    18.         NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
    19.         Debug.Log(playerControllerId);
    20.  
    21.  
    22.     }
    23.  
    24.  
    25. }
    26.  
    Here is a part of my player controller script with my player prefab.

    Code (CSharp):
    1.  void Start()
    2.     {
    3.         player = GetComponent<CharacterController>();
    4.         //fpsCam = GetComponentInParent<Camera>(); //
    5.         startPos = transform.position;
    6.  
    7.         if (!isLocalPlayer)
    8.         {
    9.             Destroy(this);
    10.             pigEyes.SetActive(false);
    11.             // gameObject.SetActive(false);
    12.             Debug.Log("CAMoff");
    13.             return;
    14.  
    15.         }
    16.  
    17.     }
     
  2. hyouuu

    hyouuu

    Joined:
    Nov 26, 2020
    Posts:
    42
    Hi @marcussss did you find the cause? Facing the same issue