Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question NullReferenceException: Object reference not set to an instance of an object

Discussion in 'Code Editors & IDEs' started by prototype008, Mar 9, 2022.

  1. prototype008

    prototype008

    Joined:
    Apr 28, 2021
    Posts:
    1
    Hi, I'm a beginner with unity and Visual Studio and I'm trying to work with "Learning C#..." book, but I have a problem that I can't fix no matter where I look or what I do, can someone help me?
    Code (CSharp):
    1. public class CameraBehavior : MonoBehaviour
    2. {
    3.     public Vector3 camOffset = new Vector3(0f, 1.2f, -2.6f);
    4.  
    5.     private Transform Target;
    6.  
    7.     void start()
    8.     {
    9.         Target = GameObject.Find("player").transform;
    10.     }
    11.  
    12.     void LateUpdate()
    13.     {
    14.         this.transform.position = Target.TransformPoint(camOffset);
    15.         this.transform.LookAt(Target);
    16.     }
    17. }
    this is my whole code and my error is this:

    NullReferenceException: Object reference not set to an instance of an object
    CameraBehavior.LateUpdate () (at Assets/Scrips/CameraBehavior.cs:20)

    now I'm sure I have a "player" object but somehow it doesn't find it or something
     
    Last edited: Mar 9, 2022
  2. SNoteBook

    SNoteBook

    Joined:
    Mar 21, 2022
    Posts:
    12
    The error's in the fourteenth line. Try replacing "position" with "localPosition".

    And make the first letter of the "Start" method's name uppercase (just to make sure).
     
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,342
    It's Start written as "start" that's the problem. Unity's magic methods are case-sensitive!

    @SNoteBook the "use localPosition instead of position" advice is absolutely nonsensical. In the best case OP just ignores is, in the worst case you have thoroughly confused someone trying to learn. It's like telling somebody trying to learn maths that they have forgotten the cheese grater.

    Good on you for trying to help people, but don't just wildly flail about and tell them to do random stuff that you don't understand?
     
  4. SNoteBook

    SNoteBook

    Joined:
    Mar 21, 2022
    Posts:
    12
    I'd like to ask you not to blame me senselessly. I know what I write. I had once the same problem as @prototype008's, and I solved it by replacing "position" (which was more naturally, I thought) with "localPosition".
     
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,342
    Code is not a magic spell. You don't just replace some word with some other and then it works for reasons beyond man's ken.

    If the camera script either doesn't have any parent, or has a zeroed parent, localPosition and .position are the same thing. If the camera script has a parent, OP's code would break if you used the local position, as
    Target.TransformPoint(offset)
    gives the world coordinates the camera should be at, and the local position is relative to the parent object.


    So that your thing worked when you changed that is irrelevant, you gave the advice without understanding what OP's code is doing, or how your advice would effect that.
     
  6. SNoteBook

    SNoteBook

    Joined:
    Mar 21, 2022
    Posts:
    12
    @Baste I'm just sharing my experience. If you have more — good riddance, you'll help many people!