Ситауция: Пытюась создать словный СаундМенеджер для игры, что бы была возможность Включить или Отключать звуки в игре в любой момент. Изначально встрял с включением звука при переходе с одного левла на другой, т.е. в МэйнМеню звук вырубался, а при включении 1 лвл он снова влюкчался автоматом. Решение вроде нашлось, одако теперь я постоянно выхватываю "UnassignedReferenceException: The variable soundOffButton of SoundsManager has not been assigned.
You probably need to assign the soundOffButton variable of the SoundsManager script in the inspector." и не понимаю почему. На сколько я понимаю это ошибка которая вылетает если не назначен как либо обьект, однако всё назначено и все на местах. Ещё больше озадачивает тот факт что в МейнМеню всё работает коректно и никаких ошибок не выдает.
Ниже привожу код.
В soundOffButton и soundOnButton назначены соответсвующие кнопки которые выполняют соотвественно SoundsOff и SoundsOn. Данные кнопки размещеные в Пустой объект и создан префаб. Это префаб используется и в МейнМеню и в Левел1, однако в первом все отробатыет, а во втором Юнити ругается. В чем может быть причина? Благодарю за внимание.
Синтаксис:
Используется csharp
public class SoundsManager : MonoBehaviour
{
public GameObject soundOffButton;
public GameObject soundOnButton;
public List<AudioSource> audioSource = new List<AudioSource>();
private void Awake()
{
FindSoundOffOnButtons();
if (GameData.Instance.isMuted)
{
Debug.Log("Is muted");
SoundsOff();
}
}
public void SoundsOff()
{
for (int i = 0; i < audioSource.Count; i++)
{
audioSource[i].enabled = false;
}
Image offButton = soundOffButton.GetComponent<Image>();
offButton.enabled = false;
Image onButton = soundOnButton.GetComponent<Image>();
onButton.enabled = true;
GameData.Instance.isMuted = true;
Debug.Log("Sounds Off");
}
public void SoundsOn()
{
for (int i = 0; i < audioSource.Count; i++)
{
audioSource[i].enabled = true;
}
Debug.Log("Get try to turn on");
Image offButton = soundOffButton.GetComponent<Image>();
offButton.enabled = true;
Debug.Log("Turn on");
Image onButton= soundOnButton.GetComponent<Image>();
onButton.enabled = false;
GameData.Instance.isMuted = false;
Debug.Log("Sounds On");
}
private void FindSoundOffOnButtons()
{
Debug.Log("Find buttons");
soundOffButton = GameObject.Find("SoundOffButton");
soundOnButton = GameObject.Find("SoundOnButton");
Image onButton = soundOnButton.GetComponent<Image>();
onButton.enabled = false;
}
}
{
public GameObject soundOffButton;
public GameObject soundOnButton;
public List<AudioSource> audioSource = new List<AudioSource>();
private void Awake()
{
FindSoundOffOnButtons();
if (GameData.Instance.isMuted)
{
Debug.Log("Is muted");
SoundsOff();
}
}
public void SoundsOff()
{
for (int i = 0; i < audioSource.Count; i++)
{
audioSource[i].enabled = false;
}
Image offButton = soundOffButton.GetComponent<Image>();
offButton.enabled = false;
Image onButton = soundOnButton.GetComponent<Image>();
onButton.enabled = true;
GameData.Instance.isMuted = true;
Debug.Log("Sounds Off");
}
public void SoundsOn()
{
for (int i = 0; i < audioSource.Count; i++)
{
audioSource[i].enabled = true;
}
Debug.Log("Get try to turn on");
Image offButton = soundOffButton.GetComponent<Image>();
offButton.enabled = true;
Debug.Log("Turn on");
Image onButton= soundOnButton.GetComponent<Image>();
onButton.enabled = false;
GameData.Instance.isMuted = false;
Debug.Log("Sounds On");
}
private void FindSoundOffOnButtons()
{
Debug.Log("Find buttons");
soundOffButton = GameObject.Find("SoundOffButton");
soundOnButton = GameObject.Find("SoundOnButton");
Image onButton = soundOnButton.GetComponent<Image>();
onButton.enabled = false;
}
}