Weird NullReferenceException?

I’m getting this error every frame for a reason that I don’t know

NullReferenceException: Object reference not set to an instance of an object UIManager.Update () (at Assets/Scripts/UIManager.cs:42)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
#if UNITY_EDITOR
using UnityEditor;
#endif

public class UIManager : MonoBehaviour
{

    [SerializeField] bool volumeUp;
    public GameObject Main;
    public GameObject ControlMenu;
    public GameObject LoadingScreen;
    public GameObject MainScreen;
    public AudioSource lightNoise;
    public AudioSource beep;
    public Slider slider;
    public GameObject player;
    public PlayerController playerController;
    [SerializeField] Image HealthBar;
    [SerializeField] List<Sprite> HealthBarSprites;
    [SerializeField] List<Sprite> HungerBarSprites;
    [SerializeField] List<Sprite> WaterBarSprites;
    [SerializeField] List<Sprite> SanityBarSprites;
    [SerializeField] List<Sprite> EnergyBarSprites;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        player = GameObject.Find("Player");
        playerController = player.GetComponent<PlayerController>();

        if (slider.value <= Random.Range(0, 3) && playerController.isPaused == false)
        {
            volumeUp = true;
        }

        if (slider.value <= 10 && volumeUp == true)
        {
            slider.value += 0.125f * Time.deltaTime;
            beep.Pause();
        }

        else
        {
            volumeUp = false;
        }

        ManageBars();
    }

    public void StartGame()
    {
        SceneManager.LoadSceneAsync("Level0");
    }

    public void Quit()
    {
        #if UNITY_EDITOR
        EditorApplication.ExitPlaymode();
        #else
        Application.Quit(); // original code to quit Unity player
        #endif
    }

    public void QuitToTitle()
    {
        SceneManager.LoadScene("Title");
    }

    public void Open(GameObject page2)
    {
        page2.SetActive(true);
    }

    public void Close(GameObject page1)
    {
        page1.SetActive(false);
    }

    public void LoadLevel(int sceneIndex)
    {
        StartCoroutine(LoadAsyncronously(sceneIndex));
    }

    public IEnumerator LoadAsyncronously(int sceneIndex)
    {
        AsyncOperation operation = SceneManager.LoadSceneAsync(sceneIndex);

        LoadingScreen.SetActive(true);
        MainScreen.SetActive(false);

        while(!operation.isDone)
        {
            float progress = Mathf.Clamp01(operation.progress / 0.9f);
            slider.value = progress;

            yield return null;
        }

    }

    public void LightNoiseAdjustment()
    {
        lightNoise.volume = slider.value / 10;
        beep.volume = lightNoise.volume / 3;
        beep.Play();
    }

    public void PlayerControlStop()
    {
        playerController.canControlPlayer = false;
        Cursor.lockState = CursorLockMode.None;
        Cursor.visible = true;
    }

    public void PlayerControlPlay()
    {
        playerController.canControlPlayer = true;
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
    }

    private void ManageBars()
    {
        if(playerController.Health <= 100 && playerController.Health > 98)
        {
            HealthBar.sprite = HealthBarSprites[0];
        }

        if(playerController.Health <= 98 && playerController.Health > 96)
        {
            HealthBar.sprite = HealthBarSprites[1];
        }

        if(playerController.Health <= 96 && playerController.Health > 94)
        {
            HealthBar.sprite = HealthBarSprites[2];
        }

        if(playerController.Health <= 94 && playerController.Health > 92)
        {
            HealthBar.sprite = HealthBarSprites[3];
        }
    }
}

Thank you!

I assume this is your line 42?

if (slider.value <= Random.Range(0, 3) && playerController.isPaused == false)

Have you assigned the slider in the inspector? And is the variable playerController.isPaused public/can this script access it?

Also, it’s generally not a good idea to do this in Update():

player = GameObject.Find("Player");
playerController = player.GetComponent<PlayerController>();

It’s unnecessary to find the player gameobject again every frame, and doing so also impacts the performance. Try assigning the player and playerController in Start() or in the inspector if possible. And make sure all references are set – check the UIManager component in the inspector tab and look for empty variable references (especially the slider).