I have an error when clicking a button on the UI. After the sequence happens, I click, and error.
Error:
ArgumentNullException: Value cannot be null.
Parameter name: source
UnityEngine.AudioSource.PlayOneShot (UnityEngine.AudioClip clip, System.Single volumeScale) (at :0)
UnityEngine.AudioSource.PlayOneShot (UnityEngine.AudioClip clip) (at :0)
MemoryPuzzle.ColorButtonClicked (System.Int32 index) (at Assets/Scripts/Memory Puzzle/MemoryPuzzle.cs:97)
MemoryPuzzle+<>c__DisplayClass9_0.b__0 () (at Assets/Scripts/Memory Puzzle/MemoryPuzzle.cs:32)
UnityEngine.Events.InvokableCall.Invoke () (at <4014a86cbefb4944b2b6c9211c8fd2fc>:0)
UnityEngine.Events.UnityEvent.Invoke () (at <4014a86cbefb4944b2b6c9211c8fd2fc>:0)
UnityEngine.UI.Button.Press () (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/Button.cs:70)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/Button.cs:114)
UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:57)
UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:272)
UnityEngine.EventSystems.EventSystem:Update() (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventSystem.cs:501)
Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MemoryPuzzle : MonoBehaviour
{
public Text instructionsText;
public Button startButton;
public Button resetButton;
public AudioClip correctSound;
public AudioClip incorrectSound;
public GameObject door;
public Button colorButtons;
private List sequence;
private int currentIndex;
// Start is called before the first frame update
void Start()
{;
// Hide the reset button and door at the beginning of the game
resetButton.gameObject.SetActive(false);
// Set up the start button to initiate the game
startButton.onClick.AddListener(StartGame);
// Set up the color buttons to allow the player to input their sequence
for (int i = 0; i < colorButtons.Length; i++)
{
int buttonIndex = i;
colorButtons*.onClick.AddListener(() => ColorButtonClicked(buttonIndex));*
}
}
// Generate a random sequence of colors
void GenerateSequence()
{
sequence = new List();
for (int i = 0; i < 4; i++)
{
sequence.Add(new Color(Random.value, Random.value, Random.value));
}
}
// Display the sequence of colors to the player
IEnumerator ShowSequence()
{
instructionsText.text = “Look!”;
yield return new WaitForSeconds(1f);
GenerateSequence();
for (int i = 0; i < sequence.Count; i++)
{
instructionsText.text = "Color " + (i + 1) + “:”;
yield return new WaitForSeconds(1f);
colorButtons_.GetComponent().color = sequence*;
yield return new WaitForSeconds(1f);_
_colorButtons.GetComponent().color = Color.white;
yield return new WaitForSeconds(0.5f);
}
instructionsText.text = “MEMORIZE”;
currentIndex = 0;
}
// Handle the player clicking a color button*
void ColorButtonClicked(int index)
{
if(currentIndex >= sequence.Count)
{
return;
}
if(sequence[currentIndex] == colorButtons[index].GetComponent().color)
{
currentIndex++;
// Play a sound effect for correct input
GetComponent().PlayOneShot(correctSound);
// If the player has input the entire sequence correctly, unlock the door and display a success message
if(currentIndex == sequence.Count)
{
instructionsText.text = “Correct!”;
resetButton.gameObject.SetActive(false);
startButton.gameObject.SetActive(false);
door.SetActive(false);
}
}
else
{
// Play a sound effect for incorrect input
GetComponent().PlayOneShot(incorrectSound);
// Display an error message and reset the game
instructionsText.text = “nope :(”;
StartCoroutine(ResetGame());
}
}
// Start the game
void StartGame()
{
// Hide the start button and display the instructions
startButton.gameObject.SetActive(false);
StartCoroutine(ShowSequence());
currentIndex = 0;
}
// Reset the game
IEnumerator ResetGame()
{
// Wait for a few seconds before resetting the game
yield return new WaitForSeconds(2f);
// Hide the reset button and display the start button
resetButton.gameObject.SetActive(false);
startButton.gameObject.SetActive(true);
// Generate a new sequence of colors
GenerateSequence();
currentIndex = 0;
}
}_