Search Unity

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

Discussion in 'Scripting' started by juliangalvarez99, May 16, 2022.

  1. juliangalvarez99

    juliangalvarez99

    Joined:
    May 19, 2021
    Posts:
    4
    Hey guys, I´m new at this but I know this is one of the most common mistakes. I got some code that works fine, but the console keeps showing me this error in the line 53 and I simply don´t know how to fix it. Could u give a hand?

    This is the message:

    NullReferenceException: Object reference not set to an instance of an object
    CameraFollow.HandleMovement () (at Assets/Scripts/CameraFollow.cs:53)
    CameraFollow.Update () (at Assets/Scripts/CameraFollow.cs:27)

    And this is my code (from CodeMonkey)

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class CameraFollow : MonoBehaviour
    7. {
    8.     private Func<Vector3> GetCameraFollowPositionFunc;
    9.     public float cameraMoveSpeed;
    10.  
    11.     public float zoomOutMin;
    12.     public float zoomOutMax;
    13.     public void Setup(Func<Vector3> GetCameraFollowPositionFunc)
    14.     {
    15.         this.GetCameraFollowPositionFunc = GetCameraFollowPositionFunc;
    16.     }
    17.  
    18.     public void SetCameraFollowPositionFunc(Func<Vector3> GetCameraFollowPositionFunc)
    19.     {
    20.         this.GetCameraFollowPositionFunc = GetCameraFollowPositionFunc;
    21.     }
    22.  
    23.  
    24.     // Update is called once per frame
    25.     void Update()
    26.     {
    27.         HandleMovement();
    28.  
    29.         if (Input.touchCount == 2)
    30.         {
    31.             Touch touchZero = Input.GetTouch(0);
    32.             Touch touchOne = Input.GetTouch(1);
    33.  
    34.             Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
    35.             Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;
    36.  
    37.             float prevMagnitude = (touchZeroPrevPos - touchOnePrevPos).magnitude;
    38.             float currentMagnitude = (touchZero.position - touchOne.position).magnitude;
    39.  
    40.             float difference = currentMagnitude - prevMagnitude;
    41.  
    42.             HandleZoom(difference - 0.05f);
    43.         }
    44.  
    45.         HandleZoom(Input.GetAxis("Mouse ScrollWheel"));
    46.  
    47.  
    48.  
    49.     }
    50.  
    51.     private void HandleMovement()
    52.     {
    53.         Vector3 cameraFollowPosition = GetCameraFollowPositionFunc();
    54.         cameraFollowPosition.z = transform.position.z;
    55.  
    56.         Vector3 cameraMoveDir = (cameraFollowPosition - transform.position).normalized;
    57.         float distance = Vector3.Distance(cameraFollowPosition, transform.position);
    58.  
    59.  
    60.         if (distance > 0)
    61.         {
    62.             Vector3 newCameraPosition = transform.position + cameraMoveDir * distance * cameraMoveSpeed * Time.deltaTime;
    63.  
    64.             float distanceAfterMoving = Vector3.Distance(newCameraPosition, cameraFollowPosition);
    65.  
    66.             if (distanceAfterMoving > distance)
    67.             {
    68.                 newCameraPosition = cameraFollowPosition;
    69.             }
    70.             transform.position = newCameraPosition;
    71.         }
    72.  
    73.     }
    74.  
    75.     private void HandleZoom(float increment)
    76.     {
    77.         Camera.main.orthographicSize = Mathf.Clamp(Camera.main.orthographicSize - increment, zoomOutMin, zoomOutMax);
    78.     }
    79. }

    Thanks in advanced
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,647
    Kurt-Dekker likes this.
  3. juliangalvarez99

    juliangalvarez99

    Joined:
    May 19, 2021
    Posts:
    4
    Thank you for the help! I was finally able to solve it