Unity中实现GameObject激活及答题对错时播放音频的方法
Hey there! Let's work through adding the audio playback features you want for your quiz game—here's a straightforward breakdown based on your existing code:
First, let's make sure you have an AudioSource component set up. You can add this to the GameObject holding your LevelControlScript2 via the Unity Inspector (it's simpler than creating it dynamically in code).
1. Play Audio When Correct/Wrong Answers Are Selected
We'll modify your existing script to trigger audio clips whenever the user picks a right or wrong answer:
Step 1: Add Audio Clip References
Add these public variables at the top of your LevelControlScript2—this lets you assign your audio files directly in the Inspector:
// Audio clips for correct and incorrect answers public AudioClip correctAnswerClip; public AudioClip wrongAnswerClip; // Reference to the AudioSource component on this GameObject private AudioSource audioSource;
Step 2: Initialize the AudioSource
Update your Start() method to grab the AudioSource component (or add it if it's missing):
void Start () { // Getting current scene build index currentSceneIndex = SceneManager.GetActiveScene ().buildIndex; // Finding game objects with tags "ToEnable" and "ToDisable" toEnable = GameObject.FindGameObjectsWithTag ("ToEnable"); toDisable = GameObject.FindGameObjectsWithTag ("ToDisable"); // Disabling game objects with tag "ToEnable" foreach (GameObject element in toEnable) { element.gameObject.SetActive (false); } // Get or add AudioSource component audioSource = GetComponent<AudioSource>(); if (audioSource == null) { audioSource = gameObject.AddComponent<AudioSource>(); } }
Step 3: Trigger Audio in Answer Methods
Modify your RightAnswer() and WrongAnswer() methods to play the corresponding audio when called:
// Method is invoked when correct answer is given public void RightAnswer() { // Play correct answer audio (if clip is assigned) if (correctAnswerClip != null && audioSource != null) { audioSource.PlayOneShot(correctAnswerClip); } // Disabling game objects that are no longer needed foreach (GameObject element in toDisable) { element.gameObject.SetActive (false); } // Turn on "correct" sign correctSign.gameObject.SetActive (true); // Invoke LoadNextLevel method in 1 second Invoke ("LoadNextLevel", 1f); } // Method is invoked if incorrect answer is given public void WrongAnswer() { // Play wrong answer audio (if clip is assigned) if (wrongAnswerClip != null && audioSource != null) { audioSource.PlayOneShot(wrongAnswerClip); } // Disabling game objects that are no longer needed foreach (GameObject element in toDisable) { element.gameObject.SetActive (false); } // Turn on "incorrect" sign incorrectSign.SetActive (true); // Re-enable disabled objects (note: this seems redundant, but we'll keep it as-is per your code) foreach (GameObject element in toDisable) { element.gameObject.SetActive (true); } triesLeft--; if(triesLeft <= 0) { // Disabling game objects that are no longer needed foreach (GameObject element in toDisable) { element.gameObject.SetActive (false); } // Turn on "incorrect" sign incorrectSign2.SetActive (true); // Invoke GotoCategories method in 1 second Invoke ("GotoCategories", 1f); } }
2. Play Audio When a GameObject Is Activated
If you want audio to play whenever a specific GameObject (like correctSign, incorrectSign, or any of your toEnable objects) is turned on, you have two easy options:
Option 1: Use a Reusable Helper Script
Create a small script (name it PlayOnEnable.cs) and attach it to any GameObject you want to trigger audio when activated:
using UnityEngine; public class PlayOnEnable : MonoBehaviour { public AudioClip activationAudio; private AudioSource audioSource; void Awake() { // Get or add AudioSource to this GameObject audioSource = GetComponent<AudioSource>(); if (audioSource == null) { audioSource = gameObject.AddComponent<AudioSource>(); } audioSource.clip = activationAudio; } void OnEnable() { // Play audio when the GameObject is set to active if (activationAudio != null && audioSource != null) { audioSource.Play(); } } }
How to use:
- Attach this script to your
correctSign,incorrectSign, etc. - Assign your desired audio clip to the
Activation Audiofield in the Inspector - Now every time you call
SetActive(true)on that GameObject, the audio will play automatically!
Option 2: Trigger Audio Directly in Your Existing Script
If you prefer to keep everything in LevelControlScript2, just add a playback line right after you enable a GameObject. For example, when turning on correctSign:
// Turn on "correct" sign correctSign.gameObject.SetActive (true); // Add this line (you'll need to declare a public AudioClip correctSignAudio first) if (correctSignAudio != null && audioSource != null) { audioSource.PlayOneShot(correctSignAudio); }
Once you've made these changes, just drag your audio clips into the corresponding fields in the Unity Inspector, and your quiz game will have the audio feedback you're looking for!
内容的提问来源于stack exchange,提问作者John Jose Mergillano

