0

So im trying to make a simple room transition by having the camera basically flip into a new area after the "player" hits the collider.

here is the errors:

NullReferenceException: Object reference not set to an instance of an object RoomMove.Start () (at Assets/Scripts/RoomMove.cs:14)

NullReferenceException: Object reference not set to an instance of an object RoomMove.OnTriggerEnter2D (UnityEngine.Collider2D player) (at Assets/Scripts/RoomMove.cs:26)

here is the code:

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class RoomMove : MonoBehaviour
{
    public Vector2 cameraChangeMax;
    public Vector2 cameraChangeMin;
    public Vector3 playerChange;
    private CameraMovement cam;
    
    // Start is called before the first frame update
    void Start()
    {
        cam = Camera.main.GetComponent<CameraMovement>();
    }

    // Update is called once per frame
    void Update()
    {

    }
    private void OnTriggerEnter2D(Collider2D player)
    {
        if (player.CompareTag("Player"))
        {
            cam.minPosition += cameraChangeMin;
            cam.maxPosition += cameraChangeMax;
            player.transform.position += playerChange;
        }
    }

}
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Have you tried using a debugger to see which object is null? – Alan Birtles Nov 28 '21 at 09:35
  • 1
    If you get the exception already in `Start` the only answer can be that there is no `Camera` in your scene with the tag `MainCamera`! The rest is just a follow up error since `cam` will never be assigned due to the exception in `Start`. – derHugo Nov 28 '21 at 14:49

0 Answers0