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. Dismiss Notice

Object reference not set to an instance of an object [help]

Discussion in 'Scripting' started by AtomicCabbage33, Oct 26, 2016.

  1. AtomicCabbage33

    AtomicCabbage33

    Joined:
    Aug 31, 2015
    Posts:
    141
    Apparently my GameManager is not set to an instance of object, despite the scene camera field being occupied in the inspector. The error only arises once I join a game and is on line 48 in the Player.cs script. One I quit run-time, the same error occurs again only this time on line 115 of the PlayerSetup.cs script. The issue seems to occur when the GameManager script has to switch from the scene camera to the player camera.

    Player script:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3. using System.Collections;
    4.  
    5. [RequireComponent(typeof(PlayerSetup))]
    6. public class Player : NetworkBehaviour {
    7.  
    8.     [SyncVar]
    9.     private bool _isDead = false;
    10.     public bool isDead
    11.     {
    12.         get { return _isDead; }
    13.         protected set { _isDead = value; }
    14.     }
    15.  
    16.     [SerializeField]
    17.     private int maxHealth = 100;
    18.  
    19.     [SyncVar]
    20.     private int currentHealth;
    21.  
    22.     [SyncVar]
    23.     public string username = "Loading...";
    24.  
    25.     public int kills;
    26.     public int deaths;
    27.  
    28.     [SerializeField]
    29.     private Behaviour[] disableOnDeath;
    30.     private bool[] wasEnabled;
    31.  
    32.     [SerializeField]
    33.     private GameObject[] disableGameObjectsOnDeath;
    34.  
    35.     [SerializeField]
    36.     private GameObject deathEffect;
    37.  
    38.     [SerializeField]
    39.     private GameObject spawnEffect;
    40.  
    41.     private bool firstSetup = true;
    42.  
    43.     public void SetupPlayer ()
    44.     {
    45.         if (isLocalPlayer)
    46.         {
    47.             //Switch cameras
    48.             GameManager.instance.SetSceneCameraActive(false);
    49.             GetComponent<PlayerSetup>().playerUIInstance.SetActive(true);
    50.         }
    51.  
    52.         CmdBroadCastNewPlayerSetup();
    53.     }
    54.  
    55. ...
    56.  
    Player setup script:

    Code (CSharp):
    1.  
    2.     void OnDisable ()
    3.     {
    4.         Destroy(playerUIInstance);
    5.  
    6.         if (isLocalPlayer)
    7.             GameManager.instance.SetSceneCameraActive(true);
    8.      
    9.         GameManager.UnRegisterPlayer(transform.name);
    10.     }
    11.  
    12. }
    13.  
     

    Attached Files:

  2. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    So it seems your GameManger.instance is null. Where is the GameMangaer being setup. Is it a script attached to an object in the scene? If it is a singleton which the .instance implies I don't understand this line:
    Why would it be switching at all.. shouldn't it be its own object? Possibly with Don'tDestroyOnLoad if you have multiple scenes.
     
  3. AtomicCabbage33

    AtomicCabbage33

    Joined:
    Aug 31, 2015
    Posts:
    141
    It's a script attached to an empty GameObject in the scene. The idea is when the player dies, a separate script attached to the player prefab calls a function in the GameManager script which switches the camera over to the SceneCamera, and then once the respawn time is finished it should switch the camera back over to the Player's camera.


    EDIT: I resolved the issue, it was a problem with the custom Network Manager script I made
     
    Last edited: Oct 26, 2016