Hello!
I’m very new to Unity and C# so I’m sorry to ask a dumb question, but I’ve done some research and I can’t make sense of this by myself.
I have a very simple scene with a capsule sitting above a plane. I need the capsule’s rotation to match that of the plane - but at runtime. I put this script in the capsule.
using UnityEngine;
using System.Collections;
public class testmove : MonoBehaviour
{
void FixedUpdate()
{
RaycastHit ground = new RaycastHit();
Physics.Raycast (transform.position, transform.forward, out ground);
transform.rotation = ground.transform.rotation;
}
}
This script works perfectly, the capsule snaps to the rotation of its “floor”. Yet, even though the script works, it throws errors at the same time. Every time FixedUpdate is called, a NullReferenceException is thrown telling me there’s an error on line 10.
Why is it giving me errors even though it’s working? How do I fix it?