我正在统一制作一个记忆拼图,当我点击我的记忆 UI 上的开始按钮时,它给了我一个错误。
错误: NullReferenceException:对象引用未设置到对象的实例 MemoryPuzzle+d__11.MoveNext ()(位于 Assets/Scripts/Memory Puzzle/MemoryPuzzle.cs:49) UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator 枚举器, System.IntPtr returnValueAddress) (at <4014a86cbefb4944b2b6c9211c8fd2fc>:0) UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator) MemoryPuzzle:StartGame()(位于 Assets/Scripts/Memory Puzzle/MemoryPuzzle.cs:102) UnityEngine.EventSystems.EventSystem:Update()(位于 Library/PackageCache/[email protected]/Runtime/EventSystem/EventSystem.cs:501)
脚本:
`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<Color> 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[i].onClick.AddListener(() => ColorButtonClicked(buttonIndex));
}
}
// Generate a random sequence of colors
void GenerateSequence()
{
sequence = new List<Color>();
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);
for(int i = 0; i < sequence.Count; i++)
{
instructionsText.text = "Color " + (i + 1) + ":";
yield return new WaitForSeconds(1f);
colorButtons[0].GetComponent<Image>().color = sequence[i];
yield return new WaitForSeconds(1f);
colorButtons[0].GetComponent<Image>().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 (sequence[currentIndex] == colorButtons[index].GetComponent<Image>().color)
{
currentIndex++;
// Play a sound effect for correct input
GetComponent<AudioSource>().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);
door.SetActive(false);
}
}
else
{
// Play a sound effect for incorrect input
GetComponent<AudioSource>().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());
}
// 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();
}
}`
我绑定了一些东西,比如询问 ChatGPT,但没有运气。它应该在按钮上生成随机颜色,你必须通过单击按钮来匹配序列来记住图案。