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 25, 2013 at 08:44 PM · guiaudioontriggerenterwaitforseconds

Help with citizen conversation script.

Well, I have been doing fine writing scripts, but conversation ones always get me. The audio isn't playing, and the GUI doesn't show up. If anyone can tell me or show me what is wrong here it would be a lot of help. (NOTE: I can sometimes get clumsy with my scripts, so forgive me if it's something obvious.)

 //The area in which you can be able to talk to the person.
 var ConversationArea : GameObject;
 //The different audio clips.
 var PlayerHello : AudioClip;
 var CitizenHello : AudioClip;
 var PlayerBeMean : AudioClip;
 var CitizenBeMeanResponse : AudioClip;
 var PlayerCompliment : AudioClip;
 var CitizenComplimentResponse : AudioClip;
 var PlayerBeNice : AudioClip;
 var CitizenBeNiceResponse : AudioClip;
 var PlayerRob : AudioClip;
 var CitizenRobResponse : AudioClip;
 var PlayerMakeSmallTalk : AudioClip;
 var CitizenMakeSmallTalkResponse : AudioClip;
 //The lengths of the audio clips.
 var PlayerHelloLength : Number;
 var PlayerBeMeanLength : Number;
 var PlayerComplimentLength : Number;
 var PlayerBeNiceLength : Number;
 var PlayerRobLength : Number;
 var PlayerMakeSmallTalkLength : Number;
 var CitizenHelloLength : Number;
 //The direction the person is facing while in conversation.
 var targetPosition : Transform;
 var damp: int = 5;
 //The style of the buttons.
 var btnTexture : GUISkin;
 var CanTalk : boolean;
 var Player : GameObject;
 var rotationAngle : Number;
  
 //On start.
 function Start () {
     CanTalk = false;
 }
 //When player enters the trigger.
 function OnTriggerEnter (ConversationArea : Collider) {
     if(Player.tag == "Player") {
         CanTalk = true;
     }
 }
 //When player exits the trigger.
 function OnTriggerExit (ConversationArea : Collider) {
     if(Player.tag == "Player") {
         CanTalk = false;
     }
 }
 function OnGUI () {
     GUI.skin = btnTexture;
 }
 if(CanTalk) {
     //If N is pressed, conversation will start.
     if (Input.GetKeyDown("N") && CanTalk) {
         //The starting "hello"s.
         audio.PlayOneShot(PlayerHello);
         yield WaitForSeconds(PlayerHelloLength);
         audio.PlayOneShot(CitizenHello);
         yield WaitForSeconds (CitizenHelloLength);
             //The different buttons.
             if (GUI.Button(Rect(250,150,50,30),"Be Mean")) {
                 audio.PlayOneShot(PlayerBeMean);
                 yield WaitForSeconds (PlayerBeMeanLength);
                 audio.PlayOneShot(CitizenBeMeanResponse);
             }
                 if (GUI.Button(Rect(250,190,50,30),"Compliment")) {
                 audio.PlayOneShot(PlayerCompliment);
                 yield WaitForSeconds (PlayerComplimentLength);
                 audio.PlayOneShot(CitizenComplimentResponse);
             }
             if (GUI.Button(Rect(250,230,50,30),"Be Nice")) {
                 audio.PlayOneShot(PlayerBeNice);
                 yield WaitForSeconds (PlayerBeNiceLength);
                 audio.PlayOneShot(CitizenBeNiceResponse);
             }
                if (GUI.Button(Rect(250,270,50,30),"Rob")) {
                 audio.PlayOneShot(PlayerRob);
                 yield WaitForSeconds (PlayerRobLength);
                 audio.PlayOneShot(CitizenRobResponse);
             }
                if (GUI.Button(Rect(250,310,50,30),"Make Small Talk")) {
                 audio.PlayOneShot(PlayerMakeSmallTalk);
                 yield WaitForSeconds (PlayerMakeSmallTalkLength);
                 audio.PlayOneShot(CitizenMakeSmallTalkResponse);
             }
     }
 }
 //Rotation, not done, work on it later.
 if (rotationAngle) {
     Quaternion.LookRotation(targetPosition.position - transform.position);
 }

The booleans work correctly, and yield WaitForSeconds has given me trouble in the past, so I'm not exactly sure what could have gone wrong. Thanks in advance! (P.S. I know that the rotation isn't done yet, I just want to get the actual conversation done first.)

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by whydoidoit · Mar 25, 2013 at 09:05 PM

Your OnGUI has an extra } on line 51 in the listing above, the code that follows is not in OnGUI (this is one reason I don't like Unity Script - because that invalid code is apparently valid, just it doesn't run when you want it to!) Furthermore you can't yield in an OnGUI function... nor can you use Input (you have to use the Event.current properties).

Also, you'd be better off making a class of those conversations and responses and then your code would be about 30% of the current size and would be far more extendable!

Additionally PlayerMakeSmallTalk.length is the length in time of PlayerMakeSmallTalk etc.

   @Serializable
   class ConversationElement 
   {
          var name : String;
          var description : String;
          var audio : AudioClip;
          var showSpeechButton = true;
   }

   var playerSpeech : ConversationElement[];
   var citizenSpeech : ConversationElement[];

   function SaySomething(var options : ConversationElement[], var phrase : String)
   {
         for(var i = 0; i < options.Length; i++)
         {
              if(options[i].name == phrase)
              {
                   audio.PlayOneShot(options[i].audio);
                   yield WaitForSeconds(options[i].audio.length);
                   break;
              }
         }
   }

   function SpeakAndRespond(var phrase : String)
   {
       CanTalk = false;
       yield SaySomething(playerSpeech, phrase);
       yield SaySomething(citizenSpeech, phrase);
       CanTalk = true;
   }

   var Talking = false;
   var CanTalk = true;

   function OnGUI()
   {
         if( Talking && CanTalk)
         {
                var y = 150;
                for(var thingToSay in playerSpeech)
                {
                      if(thingsToSay.showSpeechButton)
                      {
                        if(GUI.Button(Rect(250,y,100,30), thingToSay.description)
                        {
                            StartCoroutine(SpeakAndRespond(thingToSay.name));
                        }
                
                        y+= 40;
                    }
                }
         }
   }

   function Update()
   {
        if(Input.GetKeyDown("n") && !Talking)
        {
            Talking = true;
            StartCoroutine(SpeakAndRespond("Hello"));
        }
   }

With that you would do is use the inspector to fill out those arrays - dropping in your audio clips etc and naming them the same name for citizen and player. So e.g. name = "SmallTalk", description = "Make Small Talk", audio = some clip

Comment
Add comment · Show 1 · Share
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 whydoidoit · Mar 25, 2013 at 09:20 PM 0
Share

Or it might be nicer to have one or more responses in the ConversationElement for the citizen and player:

   @Serializable
   class ConversationElement 
   {
          var name : String;
          var description : String;
          var player : AudioClip[];
          var citizen : AudioClip[];
          var showSpeechButton = true;
   }
  
   var speech : ConversationElement[];
 
   function SaySomething(var phrase : String) : ConversationElement
   {
         for(var i = 0; i < speech.Length; i++)
         {
              if(speech[i].name == phrase)
              {
                   return speech[i];
              }
         }
         return null;
   }
  
   function SpeakAndRespond(var phrase : String)
   {
       CanTalk = false;
       var conversation = SaySomething(phrase);
       if(conversation != null)
       {
         var playerSpeech =conversation.player[Random.Range(0, conversation.player.length)]; 
         audio.PlayOneShot(playerSpeech);
         yield WaitForSeconds(playerSpeech.length);
 
         var citizenSpeech =conversation.citizen[Random.Range(0, conversation.citizen.length)]; 
         audio.PlayOneShot(citizenSpeech);
         yield WaitForSeconds(citizenSpeech.length);
       }
 
       CanTalk = true;
   }
  
   var Talking = false;
   var CanTalk = true;
  
   function OnGUI()
   {
         if( Talking && CanTalk)
         {
                var y = 150;
                for(var thingToSay in speech)
                {
                      if(thingsToSay.showSpeechButton)
                      {
                        if(GUI.Button(Rect(250,y,100,30), thingToSay.description)
                        {
                            StartCoroutine(SpeakAndRespond(thingToSay.name));
                        }
  
                        y+= 40;
                    }
                }
         }
   }
  
   function Update()
   {
        if(Input.Get$$anonymous$$eyDown("n") && !Talking)
        {
            Talking = true;
            StartCoroutine(SpeakAndRespond("Hello"));
        }
   }

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

11 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

Related Questions

Help with a simple conversation script? 0 Answers

help whit WaitForSecond use. 1 Answer

Gui Playing Audio Trouble 1 Answer

Play Audio on Collision or Trigger Enter 4 Answers

someone is there? 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