# Need Help! Can't fix this Null error.

**URL:** <https://discussions.unity.com/t/need-help-cant-fix-this-null-error/534657>\
**Category:** Scripting\
**Tags:** Scripting\
**Created:** [April 11, 2014, 8:40pm UTC](https://discussions.unity.com/t/need-help-cant-fix-this-null-error/534657 "2014-04-11T20:40:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![SalvaG](https://avatars.discourse-cdn.com/v4/letter/s/a87d85/32.png) [@SalvaG](https://discussions.unity.com/u/SalvaG)\
**Post date:** [April 11, 2014, 8:40pm UTC](https://discussions.unity.com/t/need-help-cant-fix-this-null-error/534657/1 "2014-04-11T20:40:42Z")

</div>

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 [&nbsp;] 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 [&nbsp;] spawnSpots;

// Use this for initialization  
void Start () {  
SpawnSpot [&nbsp;] spawnSpots = GameObject.FindObjectsOfType ();  
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);  
}

}

---

<div class="post-metadata">

**Author:** ![Carpe-Denius](https://avatars.discourse-cdn.com/v4/letter/c/9e8a1a/32.png) [@Carpe-Denius](https://discussions.unity.com/u/Carpe-Denius)\
**Post date:** [April 11, 2014, 8:55pm UTC](https://discussions.unity.com/t/need-help-cant-fix-this-null-error/534657/2 "2014-04-11T20:55:27Z")

</div>

instead of

void Start () {  
SpawnSpot [&nbsp;] spawnSpots = GameObject.FindObjectsOfType ();

try

void Start () {  
spawnSpots = GameObject.FindObjectsOfType ();

So you wont create a new local variable “spawnSpots” and instead use your class property

---

<div class="post-metadata">

**Author:** ![jemast](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/jemast/32/432128_2.png) [@jemast](https://discussions.unity.com/u/jemast)\
**Post date:** [April 11, 2014, 9:27pm UTC](https://discussions.unity.com/t/need-help-cant-fix-this-null-error/534657/3 "2014-04-11T21:27:43Z")

</div>

[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…

---

<div class="post-metadata">

**Author:** ![DanielQuick](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/danielquick/32/223057_2.png) [@DanielQuick](https://discussions.unity.com/u/DanielQuick)\
**Post date:** [April 11, 2014, 9:32pm UTC](https://discussions.unity.com/t/need-help-cant-fix-this-null-error/534657/4 "2014-04-11T21:32:54Z")

</div>

> [@jemast](#):
>
> 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).

Random.Range (float, float) is [inclusive, inclusive]

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

[Random.Range](http://docs.unity3d.com/Documentation/ScriptReference/Random.Range.html)

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

---

<div class="post-metadata">

**Author:** ![SalvaG](https://avatars.discourse-cdn.com/v4/letter/s/a87d85/32.png) [@SalvaG](https://discussions.unity.com/u/SalvaG)\
**Post date:** [April 12, 2014, 8:25am UTC](https://discussions.unity.com/t/need-help-cant-fix-this-null-error/534657/5 "2014-04-12T08:25:43Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![SalvaG](https://avatars.discourse-cdn.com/v4/letter/s/a87d85/32.png) [@SalvaG](https://discussions.unity.com/u/SalvaG)\
**Post date:** [April 12, 2014, 9:14am UTC](https://discussions.unity.com/t/need-help-cant-fix-this-null-error/534657/6 "2014-04-12T09:14:38Z")

</div>

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’
