Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question NullReferenceException on Slider UI Values. *Stuck

Discussion in 'Scripting' started by epicmiggygame, Aug 26, 2023.

  1. epicmiggygame

    epicmiggygame

    Joined:
    Aug 13, 2023
    Posts:
    5
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using TMPro;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6. using UnityEngine.Audio;
    7. using System;

    8. public class MusicVolume : MonoBehaviour
    9. {
    10. public TMP_Text musicVolDisplay;
    11. public TMP_Text soundEffectsDisplay;
    12. public Slider musicVolSlider;
    13. public Slider soundEffectsSlider;
    14. public GameManager gameManager;
    15. public Collect collectScript;
    16. public PlayerMovement playerMovementScript;
    17. // Start is called before the first frame update
    18. void Start()
    19. {
    20. GameObject musicVolValue = GameObject.Find("MusicVolumeDisplay");
    21. GameObject musicSlider = GameObject.Find("MusicSlider");
    22. GameObject effectVolValue = GameObject.Find("Player");
    23. GameObject effectSlider = GameObject.Find("SFXSlider");
    24. musicVolDisplay = musicVolValue.GetComponent<TMP_Text>();
    25. musicVolSlider = musicSlider.GetComponent<Slider>();
    26. soundEffectsDisplay = effectVolValue.GetComponent<TMP_Text>();
    27. soundEffectsSlider = effectSlider.GetComponent<Slider>();


    28. musicVolDisplay.text = musicVolSlider.value.ToString("0.00");
    29. if (gameManager != null || collectScript != null || playerMovementScript != null)
    30. {
    31. Debug.Log("Not Null");
    32. }
    33. else
    34. {
    35. Debug.Log("Its null");
    36. }
    37. }

    38. private void Update()
    39. {
    40. LoadMusicVol();
    41. LoadSFXVol();
    42. }

    43. public void ChangeDiplayMusicVol()
    44. {
    45. musicVolDisplay.text = musicVolSlider.value.ToString("0.00");
    46. PlayerPrefs.SetFloat("MusicVolume", musicVolSlider.value);

    47. }

    48. public void ChangeDisplaySFXVol()
    49. {
    50. soundEffectsDisplay.text = soundEffectsSlider.value.ToString("0.00");
    51. PlayerPrefs.SetFloat("SFXVolume", soundEffectsSlider.value);
    52. }



    53. public void LoadMusicVol()
    54. {
    55. float musicVolume = PlayerPrefs.GetFloat("MusicVolume");
    56. musicVolSlider.value = musicVolume;
    57. GameObject gameMan = GameObject.Find("GameManager");
    58. gameManager = gameMan.GetComponent<GameManager>();
    59. AudioSource mainMusic = gameManager.gameplayMusic;
    60. mainMusic.volume = musicVolume;
    61. }

    62. public void LoadSFXVol()
    63. {
    64. float soundEffectVolume = PlayerPrefs.GetFloat("SFXVolume");
    65. soundEffectsSlider.value = soundEffectVolume;
    66. GameObject playerSFX = GameObject.Find("Player");
    67. collectScript = playerSFX.GetComponent<Collect>();
    68. playerMovementScript = playerSFX.GetComponent<PlayerMovement>();
    69. AudioSource soundEffects1 = collectScript.collectCoin;
    70. AudioSource soundEffects2 = collectScript.coinGoal;
    71. AudioSource soundEffects3 = playerMovementScript.jump;
    72. soundEffects1.volume = soundEffectVolume;
    73. soundEffects2.volume = soundEffectVolume;
    74. soundEffects3.volume = soundEffectVolume;
    75. }
    Error is occurring on lines 66 and 76. I don't really know how to fix this because I'm still a beginner in coding. Any help is appreciated.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    Please edit your post to use code-tags when posting code; don't use plain text.

    Thanks.
     
  4. epicmiggygame

    epicmiggygame

    Joined:
    Aug 13, 2023
    Posts:
    5
    Thanks! I managed to track down what was causing the null reference and it was the SFX display text. I changed the GameObject.Find to find the actual object that contained the TMP_Text. Also thank you MelvMay for the code tags disclaimer. This was my first-ever post.:):)
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Just so you know... you're playing with a loaded gun here:

    Remember the first rule of GameObject.Find():

    Do not use GameObject.Find();

    More information: https://starmanta.gitbooks.io/unitytipsredux/content/first-question.html

    More information: https://forum.unity.com/threads/why-cant-i-find-the-other-objects.1360192/#post-8581066

    In general, DO NOT use Find-like or GetComponent/AddComponent-like methods unless there truly is no other way, eg, dynamic runtime discovery of arbitrary objects. These mechanisms are for extremely-advanced use ONLY.

    If something is built into your scene or prefab, make a script and drag the reference(s) in. That will let you experience the highest rate of The Unity Way(tm) success of accessing things in your game.


    FURTHERMORE, you're marking those public, so you may fall into this issue:

    Serialized properties in Unity are initialized as a cascade of possible values, each subsequent value (if present) overwriting the previous value:

    - what the class constructor makes (either default(T) or else field initializers, eg "what's in your code")

    - what is saved with the prefab

    - what is saved with the prefab override(s)/variant(s)

    - what is saved in the scene and not applied to the prefab

    - what is changed in Awake(), Start(), or even later etc.

    Make sure you only initialize things at ONE of the above levels, or if necessary, at levels that you specifically understand in your use case. Otherwise errors will seem very mysterious.

    Here's the official discussion: https://blog.unity.com/technology/serialization-in-unity

    Field initializers versus using Reset() function and Unity serialization:

    https://forum.unity.com/threads/sensitivity-in-my-mouselook-script.1061612/#post-6858908

    https://forum.unity.com/threads/crouch-speed-is-faster-than-movement-speed.1132054/#post-7274596