- Home /
Help with a simple conversation script?
Hi, I've been trying to make a very simple conversation script. Basically, is shows that when you enter the object's collider, some sounds play in a certain order. The only problem is that it doesn't work as planned. As in, the sounds automatically play when the scene is started, and the sounds don't play correctly (not in order). I don't know if anything is incompatible, or maybe I just missed something. If anyone can be able to help, it would be great.
#pragma strict
var AudioPlaying : boolean = false;
var YourIntroduction : AudioClip;
var Response1 : AudioClip;
var YourResponse1 : AudioClip;
var Response2 : AudioClip;
var YouSayYes : AudioClip;
var YouSayNo : AudioClip;
var ResponseIfNo : AudioClip;
var ResponseIfYes : AudioClip;
var TimeOfIntro : Number;
var TimeOfResponse1 : Number;
var TimeOfYourResponse1 : Number;
var TimeOfYouSayYes : Number;
var TimeOfYouSayNo : Number;
var TimeOfResponseIfNo : Number;
var TimeOfResponseIfYes : Number;
var TimeOfFirstPart : Number;
var HintOnWhatToSay : GUIText;
var SayYesKey : KeyCode;
var SayNoKey : KeyCode;
var SaidYes : boolean = false;
var SaidNo : boolean = false;
var SayingIsEnabled : boolean = false;
var PlayingResponse1 : boolean = false;
var PlayingYourResponse1 : boolean = false;
var PlayingResponse2 : boolean = false;
var PlayingYouSayYes : boolean = false;
var PlayingYouSayNo : boolean = false;
var PlayingResponseIfYes : boolean = false;
var PlayingResponseIfNo : boolean = false;
function Start () {
AudioPlaying = false;
SaidYes = false;
SaidNo = false;
SayingIsEnabled = false;
PlayingResponse1 = false;
PlayingYourResponse1 = false;
PlayingResponse2 = false;
PlayingYouSayYes = false;
PlayingYouSayNo = false;
PlayingResponseIfYes = false;
PlayingResponseIfNo = false;
}
function OnTriggerEnter (other : Collider) {
AudioPlaying = true;
SaidYes = false;
SaidNo = false;
SayingIsEnabled = false;
}
function Update () {
WaitForSeconds(TimeOfFirstPart);
SayingIsEnabled = true;
}
if(AudioPlaying) {
audio.PlayOneShot(YourIntroduction);
SayingIsEnabled = false;
PlayingResponse1 = true;
PlayingYourResponse1 = true;
PlayingResponse2 = true;
PlayingYouSayYes = true;
PlayingYouSayNo = true;
PlayingResponseIfYes = true;
PlayingResponseIfNo = true;
}
if(SaidYes) {
SayingIsEnabled = false;
PlayingYouSayYes = true;
PlayingResponseIfYes = true;
}
if(SaidNo) {
SayingIsEnabled = false;
PlayingYouSayNo = true;
PlayingResponseIfNo = true;
}
if(SayingIsEnabled) {
if (Input.GetKeyDown(SayYesKey))
SaidYes = true;
SaidNo = false;
if (Input.GetKeyDown(SayNoKey))
SaidYes = false;
SaidNo = true;
}
if(PlayingResponse1) {
WaitForSeconds(TimeOfIntro);
audio.PlayOneShot(Response1);
}
if(PlayingYourResponse1) {
WaitForSeconds(TimeOfIntro);
WaitForSeconds(TimeOfResponse1);
audio.PlayOneShot(YourResponse1);
}
if(PlayingResponse2) {
WaitForSeconds(TimeOfIntro);
WaitForSeconds(TimeOfResponse1);
WaitForSeconds(TimeOfYourResponse1);
audio.PlayOneShot(Response2);
}
if(PlayingYouSayYes) {
WaitForSeconds(TimeOfFirstPart);
audio.PlayOneShot(YouSayYes);
}
if(ResponseIfYes) {
WaitForSeconds(TimeOfFirstPart);
WaitForSeconds(TimeOfYouSayYes);
audio.PlayOneShot(ResponseIfYes);
}
if(YouSayNo) {
WaitForSeconds(TimeOfFirstPart);
audio.PlayOneShot(YouSayNo);
}
if(ResponseIfNo) {
WaitForSeconds(TimeOfFirstPart);
WaitForSeconds(TimeOfYouSayNo);
audio.PlayOneShot(ResponseIfNo);
}
if(ResponseIfNo) {
WaitForSeconds(TimeOfFirstPart);
WaitForSeconds(TimeOfYouSayNo);
audio.PlayOneShot(ResponseIfNo);
}
(FYI: This code was made for use in a store, so don't mind the stupid names ;))
This script will never work as it's written at the moment.
Here are just some points which are wrong / bad formatted:
You use a strange and orten wrong indention for code lines. This makes the code very hard to read and doesn't help to spot errors.
Because of the strange indention you might have missed that the major part of your code is outside Update. That means it executes once in Awake at the start of your game. (Your update function actually has only two lines in it).
WaitForSeconds is not a function, it's an object that you create (you call the constructor of the object). Such an object can only be used in a coroutine and has to be passed to the yield statement.
The Update callback can't be a coroutine, so you probably want something like a CoUpdate.
Your "Input.Get$$anonymous$$eyDown" if statements seems to miss curly brackets. The indention suggest that SaidYes and SaidNo should be assigned as a pair, but in your case only the first one is affected by the "if".
In result of the point above SaidNo will always be true when the if-statements are processed.
$$anonymous$$ost of your boolean variables get initialized 3 times. Once when the script is created (field initializer even before the constructor). Once when Unity deserialized the scene data, so it get's the values set in the inspector. And finally you set them again in Start.
I'm sill not entirely sure what the script is actually used for, however a script titled "very simple" and starts with 30 public variables sounds a bit strange.
Bunny has some seriously good advice. I also want to add that you can time audio with checking the length or if it's playing, which will narrow down some of your variables.
// Wait until 'YouSayYes' has finished playing
while (YouSayYes.isPlaying())
yield;
Thanks for the help, I didn't even realize any of those things. I guess I was a little too careless when writing this script. Thanks again! I'll continue working on it later today. (P.S. I'm writing the script for a store-like setting, and I'm planning on having an inspector-based conversation script to make it easier for later.)