- Home /
Object Reference Exception (I'm getting sick of these) :P
For the life of me I can't find the error in this code. Apparently heldBlock is not being set but I can't find the error.
using UnityEngine;
using System.Collections;
public class BlockPlacer : MonoBehaviour
{
private GameManager.Block[] blocks = new GameManager.Block[GameManager.blockSet.Length];
public float reach;
private int i = 0;
private GameManager.Block heldBlock;
private AudioSource[] sounds = new AudioSource[2];
private AudioSource placeBlock;
private AudioSource breakBlock;
void Start()
{
int i = 0;
foreach (GameManager.Block blck in GameManager.blockSet)
{
blocks[i] = blck;
i++;
}
sounds = GetComponents<AudioSource>();
placeBlock = sounds[0];
breakBlock = sounds[1];
heldBlock = blocks[0];
}
// Update is called once per frame
void Update()
{
RaycastHit hitInfo;
//Creating a block
if (Input.GetButtonDown("Fire1"))
{
Ray direction = Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));
if (Physics.Raycast(direction, out hitInfo, reach))
{
Transform oHit = hitInfo.collider.gameObject.transform;
Instantiate(heldBlock.block, new Vector3(oHit.position.x + hitInfo.normal.x, oHit.position.y + hitInfo.normal.y, oHit.position.z + hitInfo.normal.z), Quaternion.identity);
placeBlock.Play();
}
}
//Deleting a block
else if (Input.GetButtonDown("Fire2"))
{
//Get the block to be deleted
Ray direction = Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));
if (Physics.Raycast(direction, out hitInfo))
{
//Make sure it can be broken
if (hitInfo.collider.gameObject.tag == "breakable")
{
//Break the block
Destroy(hitInfo.collider.gameObject);
breakBlock.Play();
}
}
}
//Changing held block
if (Input.GetAxis("Mouse ScrollWheel") > 0)
{
i++;
if (i <= (blocks.Length - 1))
{
heldBlock = blocks[i];
}
else if (i > blocks.Length)
{
i = 0;
heldBlock = blocks[i];
}
}
else if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
i--;
if (i >= 0)
{
heldBlock = blocks[i];
}
else if (i < 0)
{
i = (blocks.Length - 1);
heldBlock = blocks[i];
}
}
}
void OnGUI()
{
GUI.Box(new Rect(10, 10, heldBlock.blockTex.width, heldBlock.blockTex.height), heldBlock.blockTex, GUIStyle.none);
}
}
the error is being thrown in OnGUI()
It's a lot of code but you need it all if you're gonna find it.
I'm getting three separate errors. The first one happens about 5 five times before I stop the game.
NullReferenceException: Object reference not set to an instance of an object BlockPlacer..ctor ()
the second and third are both logged only once
NullReferenceException: Object reference not set to an instance of an object WorldGeneration.Start () (at Assets/Scripts/WorldGeneration.cs:15)
NullReferenceException: Object reference not set to an instance of an object (wrapper stelemref) object:stelemref (object,intptr,object) BlockPlacer.Start () (at Assets/Scripts/BlockPlacer.cs:25)
Answer by Landern · Jul 09, 2014 at 04:37 PM
From you code it can only be properties in your Block type.
Since OnGUI only has one line, here are your options: heldBlock is null leaving blockText inaccessible heldBlock.blockTex is null leaving blockText.blockTex inaccessible heldBlock.blockTex.width shouldn't be nullable and default to 0, so that shouldn't be the issue.
The same is true for the fourth(height) parameter of Rect's constructor.
My guess is that heldBlock.blockTex is null and not set/instantiated.
Also, actually post a copy of the exception that is thrown, it's a lot more helpful then your title.
Thanks It was actually a problem with load order and is too complicated for me to explain lol thanks anyway.