#Error message after game ends

1 messages · Page 1 of 1 (latest)

serene hill
#

my console throws the errors in the image every time a scene is completed and a new one is invoked .It’s throwing the error in OnDisable method

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

public class akm : MonoBehaviour
{
    PlayerInput input;
    InputHandler inputHandler;
    public Transform shootPoint;
    [SerializeField] float Range;
    public int damage;
    [SerializeField] int minDamage = 20;
    [SerializeField] int maxDamage = 40;


    public int currentAmmo;
    public int maxAmmo;

    [SerializeField] float reloadTime;
    public bool isReload = false;

    public TextMeshProUGUI ammoCount;
    [SerializeField] float hitForce = 3;

    [SerializeField] GameObject impactEffect;

    public GameObject GunSprite;
    public GameObject blood;
    public float fireRate = 50;
    float nextTimeToFire;

    public ParticleSystem muzzleFlash;
    public GameObject scopeOverlay;
    public GameObject weaponCamera;
    public Camera mainCam;
    [SerializeField] float scopedView;
    public GameObject HealthBar;
    public GameObject crossHr;

    Animator animator;
    void Start()
    {
        input = GetComponent<PlayerInput>();
        inputHandler = new InputHandler();
        inputHandler.Player.Enable();
        currentAmmo = maxAmmo;
        animator = GetComponent<Animator>();

    }
    private void OnEnable()
    {
        isReload = false;
        GunSprite.SetActive(true);
        HealthBar.SetActive(true);
    }
    private void OnDisable()
    {
        GunSprite.gameObject.SetActive(false);
        HealthBar.gameObject.SetActive(true);
        scopeOverlay.gameObject.SetActive(false);
        crossHr.gameObject.SetActive(true);
    }


void Update()
    {
           //game script here
    }
       
       
night juniper
#

Please do always include any relevant error messages alongside your code. Otherwise it can very much be just as much of a guessing game as in cases when no code is given.

serene hill
night juniper
#

Unloading a scene will destroy all objects inside it. Looks like that is what's happening here - either one or all of the objects you access in OnDisable don't exist anymore when that method runs.

Just wrap each of them in their own null check and you should be good.

serene hill
#

Alright thanks