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.
![alt text][1]
As you can see in the image above, the cube has been selected via one of the sides, it is selected properly, no errors.
![alt text][2]
[1]: /storage/temp/31395-cubeselected.png
[2]: /storage/temp/31396-cubeselectedfromtop.png
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().SetSelected(true);
placeableBuildingOld = hit.collider.gameObject.GetComponent();
}
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();
}
}
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().SetSelected(true);
I am totally stumped on this. Any and all help is deeply appreciated.
↧