Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 juicebox_boy · Aug 09, 2020 at 02:49 PM · dialogue

Continue dialogue with button press?

I've searched so far and wide and just feel like a fool for not finding it. How do you get dialogue to get triggered and able to go through said dialogue with pressing of an key? I'm new, but this little thing is all I need for my super super basic demo to work correctly

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Artik2442 · Aug 09, 2020 at 03:20 PM

Didn't do it before but maybe you can assign parts of dialogue to a certain value of a variable.

If it is a text, you can do something like this:

 public float ThisTextValue;
 
 public void TextSetActive(float currentTextValue)
 {
     if (currentTextValue == ThisTextValue)
     {
         gameObject.SetActive(true);
     }
     else
     {
         gameObject.SetActive(false);
     }
 }

This script have to be assigned to all the texts. You have to choose a number for each text dialogue. 1 for the first text, 2 for the second text,...

In an empty GameObject use this script:

 private float Value;
 public GameObject Text; // assign any text with the script I've made before attached
 
 void Start()
 {
     Value = 0f;
 }
 
 public void AddNumber()
 {
     Value += 1f; // Add 1 to the value
     ChangeValue Script = Text.GetComponent<ChangeValue>(); // Get the script of the text
     Script.TextSetActive(Value); // Use the public method made in the last Script
 }

With this script, assign to the OnClick event of a button the empty GameObject and select the AddNumber Method.

You can change the different texts by different audio sources and other gameObjects.

And that should do it!

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 Artik2442 · Aug 09, 2020 at 03:21 PM 0
Share

Hope it helped!

avatar image
0

Answer by Bunny83 · Aug 09, 2020 at 04:18 PM

Well, for dialogs I would always use coroutines to actually go through the conversation. It makes everything much simpler and more linear from the code point of view.


To wait for a key press inside a coroutine you can simply do this:

 // inside a coroutine
 while (!Input.GetKeyDown(KeyCode.YourKey))
     yield return null;
 // the dialog continues here

Here the coroutine is stuck inside the while loop until the desired key is pressed. If you want to use actual UI buttons and not keyboard keys, you can use my custom yield instruction "WaitForUIButtons"


Combining the two would also be possible, though a bit more work. First we could turn the first case with the keys into a custom yield instruction

 public class WaitForKeyDown : CustomYieldInstruction
 {
     public List<KeyCode> keys;
     public override bool keepWaiting => !ShouldContinue();
     public WaitForKeyDown(params KeyCode[] aKeys)
     {
         keys = new List<KeyCode>(aKeys);
     }
     private bool ShouldContinue()
     {
         foreach(var keyCode in keys)
         {
             if (Input.GetKeyDown(keyCode))
                 return true;
         }
         return false;
     }
 }

This custom yield instruction can be simply used like this:

 yield return new WaitForKeyDown(KeyCode.A, KeyCode.B);

This would wait until either key "A" or key "B" has been pressed down. To combine several of these "conditions" we can create "logic gates" like AND and OR

 public class CombineOR : CustomYieldInstruction
 {
     public List<CustomYieldInstruction> operands;
     public override bool keepWaiting => !ShouldContinue();
     public WaitForKeyDown(params CustomYieldInstruction[] aOperands)
     {
         operands = new List<CustomYieldInstruction>(aOperands);
     }
     private bool ShouldContinue()
     {
         foreach(var operand in operands)
         {
             if (!operand.keepWaiting)
                 return true;
         }
         return false;
     }
 }

Now we could simply wait for either a key press or a UI button press at the same time

 yield return new CombineOR(new WaitForKeyDown(KeyCode.A, KeyCode.B), waitForUIButtons);

You can go crazy with utility yield instructions. Though you should keep in mind to keep it logical and simple to understand. A proper dialog system would probably take the actual dialog data / tree from an external source like a json file. However that's of course not necessary. Hardcoding the actual dialog gives more flexibility about what should happen at each step.

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

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

132 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

string problem 2 Answers

Dialogue strategy? 0 Answers

How to make dialog window? 1 Answer

VR RPG like dialogue system 1 Answer

Dialogue System only plays the same words 0 Answers


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