Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Jeejo · Mar 20, 2013 at 12:05 AM · audiowaitforsecondsifplayoneshot

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 ;))

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Mar 20, 2013 at 12:46 AM 3
Share

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.

avatar image save · Mar 20, 2013 at 12:56 AM 2
Share

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;


avatar image Jeejo · Mar 20, 2013 at 11:05 AM 0
Share

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.)

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Help with citizen conversation script. 1 Answer

Assign a clip to an AudioSource when it finishes playing. 1 Answer

Every few seconds, play an audio clip 3 Answers

Any Idea why my Explosion sound is NOT playing ? 1 Answer

I need help about audio.PlayOneShot!! 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges