Search Unity

Need Help! Can't fix this Null error.

Discussion in 'Scripting' started by SalvaG, Apr 11, 2014.

  1. SalvaG

    SalvaG

    Joined:
    Apr 10, 2014
    Posts:
    13
    I am having this error that i can't seem to fix
    NullReferenceException: Object reference not set to an instance of an object
    NetworkManager.SpawnMyPlayer () (at Assets/NetworkManager.cs:37)
    I have been looking and trying but nothing (I'm new to Unity)

    I am trying to spawn my player in the world by setting spawnpoints but it won't spawn the player in
    When i do debugging it says that SpawnSpot [] spawnSpots; = Field 'NetworkManager.spawnSpots' is never assigned to,and will always have it's default value null. So i need to know how to fix this.

    Here is my NetworkManager

    using UnityEngine;
    using System.Collections;

    public class NetworkManager : MonoBehaviour {
    SpawnSpot [] spawnSpots;

    // Use this for initialization
    void Start () {
    SpawnSpot [] spawnSpots = GameObject.FindObjectsOfType<SpawnSpot> ();
    Connect ();
    }
    void Connect () {
    PhotonNetwork.ConnectUsingSettings ("Unamed Game V001");
    }

    void OnGUI (){
    GUILayout.Label ( PhotonNetwork.connectionStateDetailed.ToString () );

    }

    void OnJoinedLobby (){
    Debug.Log ("OnJoinedLobby");
    PhotonNetwork.JoinRandomRoom();
    }

    void OnPhotonRandomJoinFailed (){
    Debug.Log ("OnPhotonRandomJoinFailed");
    PhotonNetwork.CreateRoom (null);
    }

    void OnJoinedRoom (){
    Debug.Log ("OnJoinedRoom");

    SpawnMyPlayer ();
    }
    void SpawnMyPlayer() {
    SpawnSpot mySpawnSpot = spawnSpots [ Random.Range (0, spawnSpots.Length)];
    PhotonNetwork.Instantiate("PlayerController", mySpawnSpot.transform.position, mySpawnSpot.transform.rotation, 0);
    }

    }
     
    Last edited: Apr 11, 2014
  2. Carpe-Denius

    Carpe-Denius

    Joined:
    May 17, 2013
    Posts:
    842
    instead of

    void Start () {
    SpawnSpot [] spawnSpots = GameObject.FindObjectsOfType<SpawnSpot> ();


    try

    void Start () {
    spawnSpots = GameObject.FindObjectsOfType<SpawnSpot> ();

    So you wont create a new local variable "spawnSpots" and instead use your class property
     
  3. jemast

    jemast

    Joined:
    Dec 7, 2011
    Posts:
    141
    [Mistake] Also, Random.Range min and max values are both inclusive, which means you need to pass spawnSpots.Length - 1 as the max value or you could get an out of bounds exception (or whatever C# exception throws for this type of error).

    EDIT: And for safeness' sake you might want to check that spawnSpots.Length is > 0.

    EDIT2: Sorry about that, indeed its exclusive for int. Quite odd...
     
    Last edited: Apr 11, 2014
  4. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Random.Range (float, float) is [inclusive, inclusive]

    Random.Range (int, int) is [inclusive, exclusive)

    Random.Range

    The code uses Random.Range (int, int) so it is correct as it is.
     
  5. SalvaG

    SalvaG

    Joined:
    Apr 10, 2014
    Posts:
    13
    Carpe Denius the player spawns now thank you very much for that. But now my player is falling through the ground.

    (EDIT) I bumped the spawnpoints up so I don't fall through the world. BTW I used cubes to make the spawnpoints if i wanted to make them invisible would i need to delete the Mesh Renderer?
     
    Last edited: Apr 12, 2014
  6. SalvaG

    SalvaG

    Joined:
    Apr 10, 2014
    Posts:
    13
    Alright so I fixed the spawns now I have 1 more problem is that my player isn't able to jump. I know how to setup keys but when i type in like 'Spacebar' it auto gets removed. So what keybind does jump have on Unity?

    (EDIT) NVM I found the keybind it is 'space'
     
    Last edited: Apr 12, 2014