Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Network Identity on non-network game disables player prefabs

Discussion in 'Multiplayer' started by sstrong, Jan 27, 2016.

  1. sstrong

    sstrong

    Joined:
    Oct 16, 2013
    Posts:
    2,198
    I have a game that needs to run in standalone and multiplayer network modes. When the player prefab is instantiated in the standalone mode, the gameobject gets disabled (because it has the Network Identity script attached).

    How do I fix this behavior?
     
  2. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,732
    You can do something like this

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using UnityEngine.Networking;
    5.  
    6. public class SinglePlayerMode : MonoBehaviour {
    7.     [SerializeField] GameObject playerPrefab;
    8.  
    9.     void Start () {
    10.         var player = Instantiate<GameObject>(playerPrefab);
    11.         player.GetComponent<NetworkIdentity>().enabled = false;
    12.         player.SetActive(true);
    13.     }
    14. }
     
  3. SuperNeon

    SuperNeon

    Joined:
    Mar 16, 2014
    Posts:
    85
    It is better to run the single mode like the multiplayer mode or you will get a headache to support both.
     
  4. sstrong

    sstrong

    Joined:
    Oct 16, 2013
    Posts:
    2,198
    "Disabling" the NetworkIdentity seems to have no effect. For a player prefab that returns a Transform, SetActive on the gameobject (with a NetworkIdentity attached) doesn't seem to work - it is like the NetworkIdentity script is still diabling the player.
    Code (CSharp):
    1. player.GetComponent<NetworkIdentity>().enabled = false;