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

Other Null Reference Exception with all variables having a value

Discussion in 'Scripting' started by Begard, Dec 15, 2022.

  1. Begard

    Begard

    Joined:
    Dec 12, 2022
    Posts:
    4
    NullReferenceException: Object reference not set to an instance of an object
    move_camera.Update () (at Assets/move_camera.cs:37)

    I have met on this error and can't seem to find where the error is, could anyone please help me out

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class move_camera : MonoBehaviour
    4. {
    5.     public float yvalue;
    6.     public Vector3 campos;
    7.  
    8.     // Declare the player variable at the top of the class
    9.     GameObject player;
    10.     GameObject camera;
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         // Initialize the player variable when the script is first loaded
    16.         player = GameObject.FindWithTag("Player");
    17.  
    18.         Debug.Log("Player object: " + player);
    19.         Debug.Log("Player object: " + camera);
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.         // If the player object was found, get its y position
    26.         if (player != null)
    27.         {
    28.             yvalue = player.transform.position.y;
    29.  
    30.             Vector3 campos2 = new Vector3(0, 9, -10);
    31.  
    32.             // Change camera position based on player position
    33.             if (yvalue > 6)
    34.             {
    35.                 Debug.Log("Player object: " + player);
    36.                 Debug.Log("Player object: " + camera);
    37.                 Camera.main.transform.position = campos2;
    38.  
    39.             }
    40.         }
    41.     }
    42. }
    I have tested and both "player" and "camera" still exists when the error happens
     
  2. Begard

    Begard

    Joined:
    Dec 12, 2022
    Posts:
    4
    errors.png
    This is the console, the first two are when the game starts and the last three is when yvalue > 6
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
  4. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,596
    Kurt-Dekker likes this.
  5. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,571
    You tested that you assigned a camera to your
    camera
    variable, yes, but you don't use this variable anywhere. Instead you use the static shortcut property
    Camera.main
    which searches for the first camera that is tagged "MainCamera". If there is no such camera, it would return null. Your line numbers seems to be off since the error says the exception happens in line 40. So check your actual source file and check the actual line it tells you. I would bet that it's the Camera.main line and that you don't have a camera that is tagged MainCamera.
     
    Kurt-Dekker likes this.