Errors when trying to pause audio with game ()
I've been working on a script that pauses the game. I wanted the music to pause with the game, So I added a reference to the camera's AudioSource in the script, and set it to pause and unpause with the game. However, when trying to test the game afterwards, it spits out this error:
NullReferenceException: Object reference not set to an instance of an object PauseScript.Update () (at Assets/Scripts/PauseScript.cs:28)
I went back and checked the script, but I can't figure out what the issue is. There is a warning that the camera reference is never used, but it looks used to me. Any help?
BTW Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PauseScript : MonoBehaviour {
public bool paused;
private Camera cam;
// Use this for initialization
void Start () {
paused = false;
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown ("Select")) {
paused = !paused;
SoundManagerScript.PlaySound ("pause");
}
if (paused) {
Time.timeScale = 0;
cam.GetComponent<AudioSource>().Pause();
} else if (!paused) {
Time.timeScale = 1;
cam.GetComponent<AudioSource>().UnPause();
}
}
}
Answer by SolarChaser13 · Jun 23, 2017 at 01:02 AM
Update: I've discovered the error, I forgot to add the reference in the Start function, it's all good now!
Your answer
Follow this Question
Related Questions
How to do a wall jump? i am very novice 0 Answers
[HDRP] 2019.1b -> 2019.2a Errors (HDRP related I think? and others) 1 Answer
how to connect external display to second camera in unity? 0 Answers
Several music tracks on camera controlled by AudioSource.mute 1 Answer
Determine end of audio clip if paused and resumed inbetween? 1 Answer