Accept/Abandon/Fail Quest sound
-
- Posts: 5
- Joined: Fri Mar 20, 2020 4:19 pm
Accept/Abandon/Fail Quest sound
Is there a way to have a sound play when you accept a quest (same sound for all quests)? I would also know if it's possible to play a sound when you fail or abandon a quest. Thanks!
Re: Accept/Abandon/Fail Quest sound
Hi,
Yes. The easiest way (if you don't mind a tiny bit of scripting) is to add a script to the Dialogue Manager that has an OnQuestStateChange method. Something like:
Yes. The easiest way (if you don't mind a tiny bit of scripting) is to add a script to the Dialogue Manager that has an OnQuestStateChange method. Something like:
Code: Select all
using UnityEngine;
using PixelCrushers.DialogueSystem;
public class QuestStateSounds : MonoBehaviour
{
// Assign the variables below in the inspector.
public AudioSource audioSource;
public AudioClip acceptedSound;
public AudioClip failedSound;
public AudioClip abandonedSound;
void OnQuestStateChange(string quest)
{
switch (QuestLog.GetQuestState(quest))
{
case QuestState.Active:
audioSource.PlayOneShot(acceptedSound);
break;
case QuestState.Failure:
audioSource.PlayOneShot(failedSound);
break;
case QuestState.Abandoned:
audioSource.PlayOneShot(abandonedSound);
break;
}
}
}
-
- Posts: 5
- Joined: Fri Mar 20, 2020 4:19 pm
Re: Accept/Abandon/Fail Quest sound
Thank you so much for the quick reply and the script! However once I tried adding it to my game directory I get the following four console errors:
Assets\Scripts\QuestStateSounds.cs(17,28): error CS1003: Syntax error, ':' expected
Assets\Scripts\QuestStateSounds.cs(17,28): error CS1513: } expected
Assets\Scripts\QuestStateSounds.cs(23,28): error CS1003: Syntax error, ':' expected
Assets\Scripts\QuestStateSounds.cs(23,28): error CS1513: } expected
Any idea why that's happening, I tried replacing ';' with ':' but no luck. Thanks again!
Assets\Scripts\QuestStateSounds.cs(17,28): error CS1003: Syntax error, ':' expected
Assets\Scripts\QuestStateSounds.cs(17,28): error CS1513: } expected
Assets\Scripts\QuestStateSounds.cs(23,28): error CS1003: Syntax error, ':' expected
Assets\Scripts\QuestStateSounds.cs(23,28): error CS1513: } expected
Any idea why that's happening, I tried replacing ';' with ':' but no luck. Thanks again!
-
- Posts: 5
- Joined: Fri Mar 20, 2020 4:19 pm
Re: Accept/Abandon/Fail Quest sound
Actually, I found the errors and fixed the script. But the audio doesn't seem to play when attached to the Dialogue Manager even when I add an AudioSource component.
Code: Select all
using UnityEngine;
using PixelCrushers.DialogueSystem;
public class QuestStateSounds : MonoBehaviour
{
// Assign the variables below in the inspector.
public AudioSource audioSource;
public AudioClip acceptedSound;
public AudioClip failedSound;
public AudioClip abandonedSound;
void OnQuestStateChanged(string quest)
{
switch (QuestLog.GetQuestState(quest))
{
case QuestState.Active:
audioSource.PlayOneShot(acceptedSound);
break;
case QuestState.Failure:
audioSource.PlayOneShot(failedSound);
break;
case QuestState.Abandoned:
audioSource.PlayOneShot(abandonedSound);
break;
}
}
}
Re: Accept/Abandon/Fail Quest sound
Sorry, you fixed two typos (missing ':'), but the third one was more subtle. The method name is "OnQuestStateChange", not "OnQuestStateChanged". I fixed the typos in my post above.
If it still doesn't work, please check:
If it still doesn't work, please check:
- Did you assign the Audio Source field in the inspector?
- Is the Audio Source's Spatial Blend set to 2D?
- Are there any errors or warnings in the console when the audio should be playing?
-
- Posts: 5
- Joined: Fri Mar 20, 2020 4:19 pm
Re: Accept/Abandon/Fail Quest sound
Awesome, it works! Thank you so much. Great asset by the way!
Re: Accept/Abandon/Fail Quest sound
Thanks! Glad to help.