I’ve the same script as the course but, when i run the game and the level loader move me from the splash screen to the start screen, my console says “Object reference not set an instance of an object” (line 19 of the option controller). when I go to the option scene and try to move the slider the console says the same thing but for the music manager (look at the photo).
my codes are:
music player
> 1. using UnityEngine;
> 2. using System.Collections;
> 3. public class MusicPlayer : MonoBehaviour {
> 4. static MusicPlayer instance = null;
> 5. public AudioClip[] levelMusicIsChangeArray;
> 6. private static AudioSource audioSource;
> 7. void Awake()
> 8. {
> 9. DontDestroyOnLoad(gameObject);
> 10. }
> 11. private void Start()
> 12. {
> 13. audioSource = GetComponent<AudioSource>();
> 14. audioSource.volume = PlayerPrefsManager.GetMasterVolume();
> 15. }
> 16. private void OnLevelWasLoaded(int level)
> 17. {
> 18. AudioClip thisLevelMusic = levelMusicIsChangeArray[level];
> 19. Debug.Log("Playing Clip on Level " + thisLevelMusic);
> 20. if (thisLevelMusic)
> 21. {
> 22. audioSource.clip = thisLevelMusic;
> 23. audioSource.loop = true;
> 24. audioSource.Play();
> 25. }
> 26. }
> 27. public void SetVolume(float volume)
> 28. {
> 29. audioSource.volume = volume;
> 30. }
> 31. }
option controller
1. > using System.Collections;
2. > using System.Collections.Generic; 3. > using UnityEngine.UI; 4. > using UnityEngine; 5. > public class OptionController : MonoBehaviour { 6. > public Slider volumeSlider, difficultySlider; 7. > public LevelManager levelManager; 8. > private static MusicPlayer musicPlayer; 9. > // Use this for initialization 10. > void Start () { 11. > GameObject.FindObjectOfType<MusicPlayer>(); 12. > volumeSlider.value = PlayerPrefsManager.GetMasterVolume(); 13. > difficultySlider.value = PlayerPrefsManager.GetDifficulty(); 14. > } 15. > // Update is called once per frame 16. > void Update () { 17. > musicPlayer.SetVolume(volumeSlider.value); 18. > } 19. > public void SaveAndExit() 20. > { 21. > PlayerPrefsManager.SetMasterVolume(volumeSlider.value); 22. > PlayerPrefsManager.SetDifficulty(difficultySlider.value); 23. > levelManager.LoadLevel("Start Menu"); 24. > } 25. > public void SetDefaults() 26. > { 27. > volumeSlider.value = 0.8f; 28. > difficultySlider.value = 2f; 29. > } 30. > }
what should I do?
I have unity 5.6.3