Getting NullReferenceExpection when I've built and run...

So when I’m in the editor it’s all fine, no errors at all but when I build and run it’s just a black screen, I turned on the dev console and saw that I got “NullReferenceExpection: Object reference not set to an instance of an object”. I know what the error means when in the editor but I’ve never got it after I built the game. :confused:

I’m currently on a mac with the new os, maybe that’s why it’s not working? I’ve tried all the mac build options. Gonna try on my pc later today but if someone knows what may cause this I would be happy to know!

EDIT: It’s not working with the webplayer verision as well :confused:

EDIT 2: Added a 1 of how it looks when in the editor, building and playing the build.

EDIT 3: So I found out how to check the logs and found this:

NullReferenceException: Object reference not set to an instance of an object
at Brick.Update () [0x00068] in /Users/Something37/BrickBuster/Assets/Scripts/Brick.cs:36

(Filename: /Users/Something37/BrickBuster/Assets/Scripts/Brick.cs Line: 36)

Here is the code for that script:

using UnityEngine;
using System.Collections;

public class Brick : MonoBehaviour {
	public int health;
	public Material[] materialFromHealth;
	public float moveSpeed;
	public GameObject[] powerups;

	int scoreBoost = 1;
	GameObject ls;
	LevelSystem system;


	void Start(){
		ls = GameObject.FindWithTag("LevelSystem");
		system = ls.GetComponent<LevelSystem>();
	}


	void Update () {
		switch(health){ //Check how much health the brick has and change texture after that
			case 1:
			renderer.material = materialFromHealth[0];
			break;

			case 2:
			renderer.material = materialFromHealth[1];
			break;

			case 3:
			renderer.material = materialFromHealth[2];
			break;
		}

		if(system.ScoreBooster)scoreBoost=2;
		else if(!system.ScoreBooster)scoreBoost=1;

		if(health<=0){ //Checks if the brick is dead
			system.score += 40 * scoreBoost;
			system.bricksDestroyed += 1;
			system.PlayDeathSound();
			int powerup = Random.Range(0,15);
			if(powerup==0)Instantiate(powerups[0], transform.position, transform.rotation);
			if(powerup==1)Instantiate(powerups[1], transform.position, transform.rotation);
			if(powerup==2)Instantiate(powerups[2], transform.position, transform.rotation);
			if(powerup==3)Instantiate(powerups[3], transform.position, transform.rotation);
			if(powerup==4)Instantiate(powerups[4], transform.position, transform.rotation);
			if(powerup==5)Instantiate(powerups[5], transform.position, transform.rotation);
			Destroy(gameObject);
		}
		else if(system.LevelComplete)Destroy(gameObject);
	}


	void OnCollisionEnter(Collision other){
		if(other.gameObject.tag=="Player"){
			system.score += 10 * scoreBoost;
			health-=1;
		}

		foreach(ContactPoint contact in other.contacts){
			if(contact.thisCollider == collider){
				float sideSpeed = contact.point.x - transform.position.x;
				contact.otherCollider.rigidbody.AddForce(100f * sideSpeed,0,0);
			}
		}
	}
}

This is row 36:

        if(system.ScoreBooster)scoreBoost=2;
        else if(!system.ScoreBooster)scoreBoost=1;

//Elis

Have you made sure to include all scenes that are used when building?

Can you post the code where the error occurs please ? Since you know what this error means, did you try the basics to debug it ? (Debug.Log, breakpoints ...) ? Is it a multiplayer problem ?

The scene is added in the build, and no, I cant post the code where the error occurs, in the editor it's all fine and I can play the game, however when I build the game (with dev console enabled) it's a black screen and the console is spammed with "NullReferenceExpection: Object reference not set to an instance of an object", nothing else, nothing about a certain script or something :/

You should still be able to run the [debugger][1] attached to the build. Have you tried that? [1]: http://docs.unity3d.com/Documentation/Manual/Debugger.html

Hmm, didn't know about that. However I can't really figure out how it works with the new monodevelop. Would need to debug my whole build then since it works fine in the editor. :/

1 Answer

1

I can’t see any problems with your code, but if the line if(system.ScoreBooster)scoreBoost=2; is failing with a NullReferenceException, that suggests to me that “system” is not being assigned correctly in the Start() function:

ls = GameObject.FindWithTag("LevelSystem");
system = ls.GetComponent<LevelSystem>();

So, do you definitely have a gameobject in the scene tagged “LevelSystem” and does it have a script component of type “LevelSystem”? The fact that this works in editor but not in build makes me think it could be a timing issue - are any of these object created dynamically and perhaps are not instantiated at the point you’re trying to reference them?

As a test, try commenting out the lines above and instead expose “system” as a public variable and manually assign its value in the inspector - does that fix the problem?

The error is quite clear. GameObject.FindWithTag("LevelSystem") did not work and the line after that you try to use the variable, which gives you the error (line 17). Put a screenshot of the inspector when you select the object that you think is tagged "LevelSystem". This game object should be in the same scene that the game object that contains this script.

Im quite sure that it's something else since I deleted that part of the code and remade it so I wouldn't need to find it and even so (as the code doesn't exist) it gives me the same error. Everything is in the same scene and everything works in the editor. I evend made the system variable public and assigned the script to it in the inspector.

How can you get the same error if you delete this part of the code ? Unless you are looking at the wrong script, the line number should have changed !

Well don't aske me, I'm the one asking you!

If you deleted the line 17, there is something else at line 17 that generates this error. You need to tell us what is this new line.