Hi
I’m learning C# at the same time as Unity so it’s a bit of an uphill struggle as most examples are in Javascript. Anyway, I wanted to make a script that can tell me the name of the object I’m walking on. The script is attached to the first person camera.
The following code works, but if the ray doesn’t intersect the floor, for example when I jump, it gives an error. All I need to do is stop that error. I can’t stop it by making the ray longer because there are things on the next floor that it could touch. The ray needs to be the length it is.
using UnityEngine;
using System.Collections;
public class GetSectionName : MonoBehaviour {
void Update () {
Vector3 dwn = transform.TransformDirection(0,-1,0);
RaycastHit hit = new RaycastHit();
if (Physics.Raycast(transform.position, dwn, out hit, 2)); {
print (hit.collider.name);
}
}
}
What I need is way to deal with the NullReferenceException: Object reference not set to an instance of an object