[SOLVED] NullReferenceException

I got this error every time the ball hit the bricks.


Here’s my code:

Brick.cs
using UnityEngine;
using System.Collections;

public class Brick : MonoBehaviour {
public int maxHits;
int timesHit;
private Manager levelManager;

// Use this for initialization
void Start () {
	timesHit = 0;
	levelManager = GameObject.FindObjectOfType<Manager>();
}

// Update is called once per frame
void Update () {
}


void OnCollisionEnter2D (Collision2D col) {
	timesHit++;
	SimulateWin();
}

// TODO Remove this method once we can actually win!

void SimulateWin() {
	levelManager.LoadNextLevel();
	
}

}

Manager.cs
using UnityEngine;
using System.Collections;

public class Manager : MonoBehaviour {

public void LoadLevel(string name){
	Debug.Log ("Level load requested for: " +name);
	Application.LoadLevel (name);
}

public void Quit(){
	Debug.Log ("you quit!");
	UnityEditor.EditorApplication.isPlaying = false;
	Application.Quit();
}

public void LoadNextLevel(){
	Application.LoadLevel (Application.loadedLevel + 1);
}

}

Hi,

Sorry but I can’t find any problem in your scripts it looks correct, maybe you can show us the brick object in the editor.

1 Like

I am sorry. I forgot to check the stick mark on Brick’s script. They are all disable.

1 Like

Hehe… Good spot and well done for resolving it yourself :slight_smile:

1 Like

Privacy & Terms