- Home /
Accessing components through a collision
I don't like quoting errors, but I have no idea how to solve this one:
"NullReferenceException: Object reference not set to an instance of an object"
...followed by a whole load of bits that start with "Boo.Lang.Runtime.....etc"
All I am trying to do is set up an RTS-style selecting system where you click one something to select it. I have tried doing it several different ways (which is why the code below is a bit convaluted) but keep getting the same sort of error. I thought it was something to do with not accessing the right game object in the hierachy, but now I'm just confused.
My current workings:
In the main camera:
var ray : Ray;
var hit : RaycastHit;
private var clickedObject;
function Update () {
if (Input.GetButton("Left Click")) {
var layerMask = 1 << 8;
layerMask = ~layerMask;
ray = camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray,hit,100,layerMask)) {
hit.collider.transform.selectControl.selected = true;
}
}
}
and in the clicked object, which actually is Tower/Tower (one a child of the other). Both the code and the collider are in the child.
var selected : boolean;
private var ring : boolean;
var selectRing : Transform;
function Select () {
if (selected) {
if (!ring) {
Instantiate(selectRing,Vector3(0,0.001,0),transform.rotation);
ring = true;
}
}
}