public void levelOver(){
print ("check"); // this isn't triggering
if(Bricks.bricks<=0){
LoadNextLevel();}
}
in my bricks.cs i have this
void OnCollisionEnter2D(Collision2D collision){
if(isbreakable){
handlehits();}
}
void handlehits(){
timesHit++;
int maxHits=hitSprites.Length; // i don't need to set it to length +1 because of the way i worked with my sprites.
if (timesHit>=maxHits){
bricks--; // bricks count
levelManager.levelOver();
Destroy(gameObject);
}
else {
loadSprite();
}
}
i’ve been getting this message “Object reference not set to an instance of an object” even though my code is pretty close to the one on the video =(
using UnityEngine;
using System.Collections;
public class Bricks : MonoBehaviour {
public int maxHits;
public Sprite[] hitSprites;
public static int bricks=0;
public LevelManager levelManager;
private int timesHit=0;
public bool isbreakable;
void Start(){
The error i mentioned appears whenever a bricks is supposed to be destroyed and the levelOver() (or BrickDestroyed(), as they use) function should be called.
The brick doesn’t even get destroyed if i leave the code like this.
if (timesHit>=maxHits){
bricks--;
levelManager.levelOver();
Destroy(gameObject);
if i leave it like THIS:
if (timesHit>=maxHits){
bricks--;
Destroy(gameObject);
levelManager.levelOver();
it gets destroyed but the error message is risen anyway
Hi @David_Spira, first thing to note, Unity requires the names of the files and classes to be the same. At the moment you have bricks.cs and Bricks as the class name.
Second thing, if you consider the order of events you are asking the code to perform here;
if (timesHit>=maxHits){
bricks--;
levelManager.levelOver();
Destroy(gameObject);
deduct 1 from the bricks count
call loadLevel() from levelManager
I’m assuming that this method loads another scene? If so, this would explain why your Destroy(gameObject) code is not called, as you have just destroyed the current scene, all of the objects in it and loaded a new one.
But when you do it around this way;
if (timesHit>=maxHits){
bricks--;
Destroy(gameObject);
levelManager.levelOver();
You are saying;
deduct 1 from the bricks count
destroy the game object
call loadLevel() from levelManager
Can you post up your levelManager code also please.
the Bricks.cs file name is actually Bricks.cs, not as i wrote before, bricks.cs =)
using UnityEngine;
using System.Collections;
public class LevelManager : MonoBehaviour {
public void LoadLevel(string name){
Application.LoadLevel(name);
}
public void LoadNextLevel(){
Application.LoadLevel(Application.loadedLevel+1);
}
public void levelOver(){
print ("check");
if(Bricks.bricks<=0){
LoadNextLevel();}
}
public void QuitRequest(){
Debug.Log("quit");
Application.Quit ();
}
}
I think you have a class variable that is getting declared but not initialized. It’s a bit difficult to say without seeing the entire class. Make sure each variable has a value before you attempt to use it. If you can’t find it, pastebin the entire file please.
Actually, it appears this forum is reformatting what I wrote, leaving the <LevelManager> part out, so I had to use '. I think the same happened to your post.
BTW, I went back over this video, and it appears that this line of code was already in his Bricks script (as you can see in the first seconds of the video) so we both missed this line somewhere in a previous lecture.