I watched a tutorial and followed it to a T however my code says that their is a null exception.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeRoom : MonoBehaviour
{
private CameraController cam;
public Vector2 cameraChange;
public Vector3 playerChange;
private CameraController Cam;
// Start is called before the first frame update
void Start()
{
Cam = Camera.main.GetComponent();
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerEnter2D(Collider2D other)
{
if(other.tag == “Player”)
{
Cam.minPos += cameraChange;
Cam.maxPos = cameraChange;
other.transform.position += playerChange;
Hello and welcome to the forum.
If you want others to help you, it’s always a good practice to help them help you. For one you could post your code using code tags, making them easier readable. There is a sticky post on it, it’s the first on this subforum. Secondly, it helps if you mention the exact error, or at least which line throws the error.
A null reference exception basically means that a variable of reference type contains nothing, but you try to access something inside of it, as if it existed. If the code is supposed to work and you copied it 1:1, then maybe your main camera has no CameraController script attached? Do you have multiple cameras? Generally speaking, it is also a good idea to avoid ‘Camera.main’ and instead reference the correct camera through the inspector.