- Home /
For some reason Object reference not set to instance of an object
I got an error in the console saying object reference not set to instance of an object. The error in the console is not very clear on telling me which thing is not set to an instance of object. It just tells me the line that causes the error. I can't figure out what's wrong with my code.
Here is the error:
NullReferenceException: Object reference not set to an instance of an object LockWallScript.OnCollisionEnter2D (UnityEngine.Collision2D col) (at Assets/EditorAssets/Scripts/LockWallScript.cs:7)
and here is my code simplified:
LockWallScript:
public class LockWallScript : MonoBehaviour {
public bool locked = true;
void OnCollisionEnter2D (Collision2D col) {
if (col.gameObject == GameObject.Find("Player")) {
Game.game.findScene (SceneManager.GetActiveScene ().name).unlockedLockWalls.Add (transform.GetSiblingIndex ());
}
}
void Start () {
locked = true;
}
}
Game class:
[System.Serializable]
public class Game {
public static Game game;
public List<SceneLayout> scenes;
public Game () {
scenes = new List<SceneLayout>();
}
public SceneLayout findScene (string nm) {
bool sceneAvailable = false;
SceneLayout scene = null;
foreach (SceneLayout i in scenes) {
if (i.name == nm) {
sceneAvailable = true;
scene = i;
}
}
if (sceneAvailable) {
return scene;
} else if (!sceneAvailable) {
return null;
}
return null;
}
}
SceneLayout class:
[System.Serializable]
public class SceneLayout {
public string name;
public List<int> unlockedLockWalls;
public SceneLayout (string nm) {
name = nm;
}
}
GameplayControlScript:
public class GameplayControlScript : MonoBehaviour {
public static GameplayControlScript gameplay;
void Awake () {
gameplay = this;
Game.game = new Game ();
Game.game.scenes.Add(new SceneLayout(SceneManager.GetActiveScene ().name))
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
Can anyone tell me what is causing the error?
Answer by Raja-Unity · Aug 10, 2017 at 06:24 PM
I just found out what was causing the error! When I create a new SceneLayout() I just have to set the unlockedLockWalls to a new List()
Here is the SceneLayout class:
[System.Serializable]
public class SceneLayout {
public string name;
public List<int> unlockedLockWalls;
public SceneLayout (string nm) {
name = nm;
unlockedLockWalls = new List<int>();
}
}
Answer by unidad2pete · Aug 10, 2017 at 12:55 AM
I think your LockWallScript dont have any reference to your game class.
public class LockWallScript : MonoBehaviour {
public bool locked = true;
public Game game;
void OnCollisionEnter2D (Collision2D col) {
if (col.gameObject == GameObject.Find("Player")) {
if(game != null)
{
if (game.worldKeys > 0 && locked) {
game.findScene (SceneManager.GetActiveScene
().name).unlockedLockWalls.Add (transform.GetSiblingIndex ());
}
}
}
}
void Start () {
locked = true;
}
}
On Inspector, drag Object with script Game to the new field.
The Game class is not supposed to be a $$anonymous$$onoBehaviour. When I added the if statement, the error was still in the console
Oh, thats right , my fault :S
But there are something on code inside of OnCollisionEnter2D code not found on your reference.
What is the code exactly on line 13 of your code?
Try add new line to check if player its found
public class LockWallScript : $$anonymous$$onoBehaviour {
public bool locked = true;
void OnCollisionEnter2D (Collision2D col) {
if (col.gameObject == GameObject.Find("Player")) {
print("Player found");
print(Game.game.findScene (Scene$$anonymous$$anager.GetActiveScene ().name);
Game.game.findScene (Scene$$anonymous$$anager.GetActiveScene ().name).unlockedLockWalls.Add (transform.GetSiblingIndex ());
}
}
void Start () {
locked = true;
}
}
try run this code, and tell me if any thing is printed on console please.
I simplified all the scripts shown so the actual line that has the error is line 7. When I printed to the console it all worked fine but the error is still there.