MissingReferenceException Error

Origin:
I’m new to coding and trying to make a simple game with c#. The problem arose when I was trying to load a new scene. When I load into the scene, the error arises.

Error Msg:
missingReferenceException: The object of type “Transform” has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object.

Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public class Controller2D : MonoBehaviour
{
[SerializeField] private float m_JumpForce = 400f;
[Range(0, 1)] [SerializeField] private float m_CrouchSpeed = .36f;
[Range(0, .3f)] [SerializeField] private float m_MovementSmoothing = .05f;
[SerializeField] private bool m_AirControl = false;
[SerializeField] private LayerMask m_WhatIsGround;
[SerializeField] private Transform m_GroundCheck;
[SerializeField] private Transform m_CeilingCheck;
[SerializeField] private Collider2D m_CrouchDisableCollider;
const float k_GroundedRadius = .2f;
private bool m_Grounded;
const float k_CeilingRadius = .2f;
private Rigidbody2D m_Rigidbody2D;
private bool m_FacingRight = true;
private Vector3 m_Velocity = Vector3.zero;

[Header(“Events”)]
[Space]

public UnityEvent OnLandEvent;

[System.Serializable]
public class BoolEvent : UnityEvent { }

public BoolEvent OnCrouchEvent;
private bool m_wasCrouching = false;

private void Awake()
{
DontDestroyOnLoad(this.gameObject);
m_Rigidbody2D = GetComponent();

if (OnLandEvent == null)
OnLandEvent = new UnityEvent();

if (OnCrouchEvent == null)
OnCrouchEvent = new BoolEvent();
}

private void FixedUpdate()
{
bool wasGrounded = m_Grounded;
m_Grounded = false;

Collider2D[ ] colliders = Physics2D.OverlapCircleAll(m_GroundCheck.position, k_GroundedRadius, m_WhatIsGround);
for (int i = 0; i < colliders.Length; i++)
{
if (colliders*.gameObject != gameObject)*
{
m_Grounded = true;
if (!wasGrounded)
OnLandEvent.Invoke();
}
}
}

public void Move(float move, bool crouch, bool jump)
{

if (!crouch)
{

if (Physics2D.OverlapCircle(m_CeilingCheck.position, k_CeilingRadius, m_WhatIsGround))
{
crouch = true;
}
}

if (m_Grounded || m_AirControl)
{
if (crouch)
{
if (!m_wasCrouching)
{
m_wasCrouching = true;
OnCrouchEvent.Invoke(true);
}
move *= m_CrouchSpeed;
if (m_CrouchDisableCollider != null)
m_CrouchDisableCollider.enabled = false;
} else
{
if (m_CrouchDisableCollider != null)
m_CrouchDisableCollider.enabled = true;
if (m_wasCrouching)
{
m_wasCrouching = false;
OnCrouchEvent.Invoke(false);
}
}
Vector3 targetVelocity = new Vector2(move * 10f, m_Rigidbody2D.velocity.y);
m_Rigidbody2D.velocity = Vector3.SmoothDamp(m_Rigidbody2D.velocity, targetVelocity, ref m_Velocity, m_MovementSmoothing);
if (move > 0 && !m_FacingRight)
{
// … flip the player.
Flip();
}
else if (move < 0 && m_FacingRight)
{
Flip();
}
}
if (m_Grounded && jump)
{
m_Grounded = false;
m_Rigidbody2D.AddForce(new Vector2(0f, m_JumpForce));
}
}
private void Flip()
{
m_FacingRight = !m_FacingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}

  • My code is indented, it is just the post :slight_smile:

Use code tags when posting code.

missingReferenceException: The object of type “Transform” has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object.

This is pretty straight forward. You’re trying to do something with a gameobject that no longer exist.

Since you have this code, that means whatever object this script is on is being moved to the new scene but it’s leaving behind a reference that one of your variables points to. Since this reference no longer exist in the new scene, you’re getting the error. So, you’ll need to figure out what object that is.

DontDestroyOnLoad(this.gameObject);

AHHH EDIT!

I found the actual reason. In my code, I check for the ground and ceiling. So, when I load the scene, it is still trying to reference the previous transforms.

Is there any easy way to fix this? Thank you for the help

8630973--1160220--upload_2022-12-2_17-48-44.png

Don’t go too far out in the weeds. This stuff is very simple to fix.

How to fix a NullReferenceException error

https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

Three steps to success:

  • Identify what is null ← any other action taken before this step is WASTED TIME
  • Identify why it is null
  • Fix that

Before you post anything else, please read on how to post code as mentioned above yet ignored and posted an image of code.

Code Tags

Want to practice? Edit your original post.