Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Why is this simple code giving errors?

Discussion in 'Scripting' started by MathewAlden, Aug 13, 2015.

  1. MathewAlden

    MathewAlden

    Joined:
    Nov 10, 2013
    Posts:
    12
    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.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class testmove : MonoBehaviour
    5. {
    6.     void FixedUpdate()
    7.     {
    8.         RaycastHit ground = new RaycastHit();
    9.         Physics.Raycast (transform.position, transform.forward, out ground);
    10.         transform.rotation = ground.transform.rotation;
    11.     }
    12. }
    13.  
    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?
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You should only attempt to access ground.transform if the raycast returns true. If it's false then it will be null.

    --Eric
     
  3. MathewAlden

    MathewAlden

    Joined:
    Nov 10, 2013
    Posts:
    12
    Oh wow. I didn't realize it was debugging hypothetical situations. Thanks for the help!
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It's not doing that; code doesn't run hypothetical situations, only what's actually happening. You're simply attempting to refer to ground.transform even in those cases when it's null. You have to check if the raycast returned true because that's the only way you know that the ground variable has valid data. Or you could check whether ground.transform is null, but it would be cleaner to just check the raycast.

    --Eric
     
  5. MathewAlden

    MathewAlden

    Joined:
    Nov 10, 2013
    Posts:
    12
    Yeah you're right. What happened is after the capsule rotated, it was just under the ground that I thought it was above, or something like that. And then I got confused and thought the debugger was super smart. But I get it now. Thanks!