I am getting some weird behaviour regarding raycasting. I have used this exact method of raycasting in two other places where it continues to have no problems. When the ray does hit, all the code executes fine and behaves as it should. I often will click the same thing more than once and eventually it will work. The problem is alot worse when I build and run the project. I have like a 50% chance of it working in the editor, and about a 5% chance working in game. It prints 'Something wrong with raycast' when it does fail.
function Update() {
if(GameGui.pcubeModeOn == true && dragOn == true){
if(Input.GetMouseButton(0) == false) {
LineRender.lineOn = false;
// cast a ray from camera to mouse position
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
var aCollider : Collider = collider;
if (aCollider.Raycast (ray, hit, 10000.0))
{
var tVoxType : int = aCollider.GetComponent(CubeInfo).cubeVoxId;
if(tVoxType == 18 && connectionAddAllowed == true) {
var LineRenderObj = GameObject.Find("Main Camera");
LineRenderObj.GetComponent(LineRender).AddConnection(LineRender.targetPowerCube , aCollider);
connectionAddAllowed = false;
}
} else {
print("Something wrong with raycast");
}
dragOn = false;
}
}
}
Any ideas what could be wrong?