I have the following script in javascript and it works:
// Only if we hit something, do we continue
var hit : RaycastHit;
// if (!
Physics.Raycast (camera.ScreenPointToRay(Input.mousePosition), hit);
// )
// {
// print("camera says no Raycast hit");
// return;
// }
if (Time.time > nextClick)
{
nextClick = Time.time + 1.0;
// Just in case, also make sure the collider also has a renderer
// material and texture
var meshCollider = hit.collider as MeshCollider;
if (meshCollider == null || meshCollider.sharedMesh == null)
{
print("There is no renderer, or mat or texture");
return;
}
//var triangleNumber = hit.triangleIndex;
var clickScript : GUIScript = GUIObject.GetComponent(GUIScript);
clickScript.RMBClick(hit);
nextClick = Time.time + 1.0;
}
But the following code in C# does not work:
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Physics.Raycast(ray, out hit);
// We only process it once a second, because the mouse button held down
// will produce a stream of clicks:
if (Time.time > nextClick)
{
nextClick = Time.time + 1.0f;
// Just in case, also make sure the collider also has a renderer
// material and texture
MeshCollider meshCollider = hit.collider as MeshCollider;
if (meshCollider == null || meshCollider.sharedMesh == null)
{
print("There is no renderer, or mat or texture");
return;
}
//var triangleNumber = hit.triangleIndex;
Navigator clickScript = GUIObject.GetComponent<Navigator>();
clickScript.RMBClick(hit);
nextClick = Time.time + 1.0f;
}
The scripts compile, but the C# prints in the debug window:
“There is no renderer, or mat or texture” indicating that it is not getting the mesh collider information from the raycast hit. After a further experiment, I find that the raycast is not registering a hit at all.
Any ideas why the same code isn’t working in C#?
Thanks!