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. Dismiss Notice

Resolved Error in camera tutorial

Discussion in 'Scripting' started by Diedir_U, Feb 2, 2021.

  1. Diedir_U

    Diedir_U

    Joined:
    Feb 2, 2021
    Posts:
    3
    Hi all,
    i just finished the camera tutorial : https://learn.unity.com/tutorial/controlling-unity-camera-behaviour?language=en#
    but an error occured at compile time.
    I double checked my code but it is as intented in the tutorial.
    Not sure what the error means neither how to correct it.

    "NullReferenceException: Object reference not set to an instance of an object
    ShipTracker.LateUpdate () (at Assets/ShipTracker.cs:32)"

    Thank you for helping.
    picture added.
     

    Attached Files:

    Last edited: Feb 2, 2021
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    It's a null error. So you have something null, but we can't help you because we can't see your code. Show us your code with code tags. ShipTracker script. I can tell you it's on line 32 you have a null value. Learn to debug these because it's such a common error.
     
    Joe-Censored likes this.
  3. Diedir_U

    Diedir_U

    Joined:
    Feb 2, 2021
    Posts:
    3
    Hi Brathnann,
    than k you for answering , here is my code

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ShipTracker : MonoBehaviour
    6. {
    7.     public Transform trackedObject;
    8.     public float maxDistance = 10;
    9.     public float moveSpeed = 20;
    10.     public float updateSpeed = 10;
    11.     [Range(0,10)]
    12.     public float currentDistance = 5;
    13.     private string moveAxis = "Mouse ScrollWheel";
    14.     private GameObject ahead;
    15.     private MeshRenderer _renderer;
    16.     private float hideDistance = 1.5f;
    17.     // Start is called before the first frame update
    18.     void Start()
    19.     {
    20.         ahead = new GameObject("ahead");
    21.         _renderer = trackedObject.gameObject.GetComponent<MeshRenderer>();
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     void LateUpdate()
    26.     {
    27.         ahead.transform.position = trackedObject.position + trackedObject.forward * (maxDistance * 0.25f);
    28.         currentDistance += Input.GetAxisRaw(moveAxis) * moveSpeed * Time.deltaTime;
    29.         currentDistance = Mathf.Clamp(currentDistance, 0, maxDistance);
    30.         transform.position = Vector3.MoveTowards(transform.position, trackedObject.position + Vector3.up * currentDistance - trackedObject.forward * (currentDistance + maxDistance * 0.5f), updateSpeed * Time.deltaTime);
    31.         transform.LookAt(ahead.transform);
    32.        _renderer.enabled = (currentDistance > hideDistance);
    33.     }
    34. }
     
  4. Diedir_U

    Diedir_U

    Joined:
    Feb 2, 2021
    Posts:
    3
    Sorry to bother you,

    i solved my mistake, i had a child object in my player which got the meshrenderer slot,
    so removing the parent solved the error, my bad , sorry for disturbing.
     
    Joe-Censored likes this.
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    Never a waste of time if you learned something and it helped you figure it out.
     
    Joe-Censored likes this.