Home > Blockchain >  Unity multiple audio with condition
Unity multiple audio with condition

Time:10-26

I have a game object with five audio clips on it as array audio1,audio2,audio3..... I also have a text object UserInput Now audio1 will play first and wait for UseInput requiring the user to reply with male or female . If the reply is male or female then proceed to audio2 else repeat audio1. Then when audio2 played, a yes or no reply is expected from the user.if the user replies Yes then proceed to audio3 and if the reply is No then repeat audio2. This is same with audio3,4,5 but finally a yes reply in audio5 will move the user another scene.

my questions are how do i make the next audio wait for userInput and how to prevent the previous audio from repeat if user did not respond.

here is my failed attempt

public AudioSource Voice;
public AudioClip[] allSpeech;
public Text userInput;
 List<string> expectedResponse = new List<string>();
void Start()
{
   expectedResponse.Add("male");
   expectedResponse.Add("female");
   Voice = GetComponent<AudioSource> ();
   Voice.clip=allSpeech[0]; 
   Voice.Play();
   Invoke("firstResponse",Voice.clip.length);
}

void firstResponse(){
    foreach (string x in expectedResponse)
    {
        if (userInput.text.Contains(x))
            {
                Voice.clip=allSpeech[1];
                Voice.Play();
            }
            else
            {
            Voice.Play();
            }
    }
}

CodePudding user response:

  • Use InputField instead of Text. So, OnValueChange and EndEdit events are available for you to check if the user typed something. Read the documentation for details.
  • Use AudioSource.PlayOneShot(clip) to play the exact clip. It's not obvious from the code which clip is played with AudioSource.Play() if you use multiple clips. So, it will be way easier for you to control what your AudioSource is going to play. Read the docs.
  • Some optimizations to increase code readability:
    • Initialize the list with List<string> expectedResponse = new List<string>{"male", "female"};
      • or better use an array if you don't change the list
    • Use custom events to control states

CodePudding user response:

I think you need to control the flow of the app using a more general way such that if your questions and answers change, you don't have to change the code.

So if I was creating this App I would do It as Below:

  1. Create a List of Questions
  2. Create a list of List of Expected Answers. (List<List> answers)
  3. Create A List of Audio clips
  4. Initialize the app with loading first Question and first audio clip.
  5. wait for user input (InputField not Text) and Submit (Button or InputField value change)
  6. Check the user input value with the first entry of Answers list to find possible match.
  7. If It Matches load the second Question and second Audio clip, else clear input field with a feed back or something.

Loading any of the questions and Audio clips can be handled through a simple function call.

  • Related