- Home /
Selecting Object From Top Causes NullReference
This is a very odd situation I am faced with here. I've been working on an RTS style selection, with the help of a tutorial from YouTube, for a game I am working on with a friend. I've never created a building placement and selection system before so off to Google and YouTube I went. Anyhow, a result I didn't expect has arisen. In it's simplest form, clicking one of the sides of a cube placed down, selects it as expected. However clicking the top of the cube returns a NullReferenceException.
I am totally baffled, I have googled the issue with no luck, I've even attached the debugger from MonoDevelop to the Unity editor so I can step through the code and see what's going on. That hasn't helped me narrow it down either. Below you will find a couple screenshots, and the code.
As you can see in the image above, the cube has been selected via one of the sides, it is selected properly, no errors.
And now, the image above shows the cube being selected from the top, however it returns a NullReferenceException. Below is the entire source code (well the relevant scripts at least)
BuildingPlacement.cs
using UnityEngine;
using System.Collections;
public class BuildingPlacement : MonoBehaviour {
private PlaceableBuilding placeableBuilding;
private Transform currentBuilding;
private bool hasPlaced = false;
public LayerMask buildingsMask;
public PlaceableBuilding placeableBuildingOld;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Vector3 m = Input.mousePosition;
m = new Vector3(m.x,m.y,transform.position.y);
Vector3 p = camera.ScreenToWorldPoint(m);
if(currentBuilding != null && !hasPlaced) {
currentBuilding.position = new Vector3(p.x,0,p.z);
if(Input.GetMouseButtonDown(0)) {
if(IsLegalPosition()) {
hasPlaced = true;
}
}
}
else {
if(Input.GetMouseButtonDown(0)) {
RaycastHit hit = new RaycastHit();
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Debug.DrawRay(ray.origin, ray.direction * 100, Color.red, 15);
if(Physics.Raycast(ray, out hit, Mathf.Infinity,buildingsMask)) {
if(placeableBuildingOld != null) {
placeableBuildingOld.SetSelected(false);
}
hit.collider.gameObject.GetComponent<PlaceableBuilding>().SetSelected(true);
placeableBuildingOld = hit.collider.gameObject.GetComponent<PlaceableBuilding>();
}
else {
if(placeableBuildingOld != null) {
placeableBuildingOld.SetSelected(false);
}
}
}
}
}
bool IsLegalPosition() {
if(placeableBuilding.colliders.Count > 0) {
Debug.Log("Cannot place building(s), too close!");
return false;
}
else {
return true;
}
}
public void SetItem(GameObject b) {
hasPlaced = false;
currentBuilding = ((GameObject)Instantiate(b)).transform;
placeableBuilding = currentBuilding.GetComponent<PlaceableBuilding>();
}
}
The only relevant piece of code from this next script is the SetSelected function
PlaceableBuilding.cs
public void SetSelected(bool s) {
isSelected = s;
}
}
The line of interest in the NullReferenceException is this one:
hit.collider.gameObject.GetComponent<PlaceableBuilding>().SetSelected(true);
I am totally stumped on this. Any and all help is deeply appreciated.
It is likely that you are hitting something other than you expect with your Raycast(). Add:
Debug.Log(hit.collider.name + ", " + hit.collider.tag);
....as the first line inside your Raycast() success code.
Ah yeah see I tried that before only I must have put it in the wrong place because it had returned a NullReferenceException and no name. This time it worked though and the issue has been resolved. Thanks. I'll post an answer of what happened and how it was fixed.
Answer by BBrown4 · Aug 23, 2014 at 09:59 PM
Alright, so with the help of robertbu I've solved the issue. What was happening was that my "buildings" are in empty gameobjects (so i can set the building's y value to 0 and have the building NOT in the ground), and these containers actually have colliders on them that act as triggers. This triggers are bigger than the object themselves, which also have regular colliders on them so they can actually enter the triggers.
On the cube however, the height of the trigger collider was the same size as the regular collider. The script is attached to the container, not the object within the container so it was hitting the regular collider before the trigger collider, and as a result returned the null reference exception because that object was unrecognized. It's not too likely that anyone will run into this EXACT issue because it's a matter of how it was set up, but just on the off chance that it does happen, here is what was wrong with mine. Thanks robertbu for your help!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
"Strange behaviour may occur" when using Network.AllocateViewID() 0 Answers
NullReferenceException problem 2 Answers
Help With AI Error 2 Answers
Need help calling a script to another keep getting errors 1 Answer