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 Pengu · Jul 05, 2010 at 11:59 PM · guibuttongui-buttonskip

button pressed twice / Conversation text skip

Hey guys,

I've got a really stupid problem...looked up the scripting reference & unity boards but didn't find a answer yet.

I'm trying to make a conversation between the player and a NPC (like in the most RPGs, running around and asking people).

Trigger, GUIs, everything fine & working until I want to skip the text with the same button!

So basically the player is colliding with the trigger-zone of the NPC. If inside and pressing the "Interact-Button" to talk, a text message appears. To skip the 1st part of the text and listen to the next, the same button is pressed again ("Interact-Button")

Here is my script which is attached to the trigger-zone (cube) around the NPC :


static var talking = false; var TextA = false; var TextB = false;

function OnTriggerStay (other : Collider) { if(other.gameObject.tag == "Player") { if(Input.GetButtonUp("Interact")) { talking = true; TextA = true; print("TextA"); // console check

         if(Input.GetButtonUp("Interact"))
         {
             TextB = true;
             print("TextB"); // console check
         }
     }
 }

}

function OnGUI () { if (talking) { if (TextA) { // GUI label is shown with Text A } else if (TextB) { // GUI label is shown with Text B } } }


If I try to talk to the NPC and press the Button "ONCE"! the console shows me TextA followed by TextB immediately :( .

It should be like: press -> TextA -> press -> TextB ->press -> TextC and so on... [of course I would set the previous Text to false for it to disappear but first things first]

I would greatly appreciate your help Uni-Community

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

5 Replies

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

Answer by Mike 3 · Jul 06, 2010 at 12:24 AM

Since everyone else has already posted about your double GetButtonUp, here's a changed version of your code which uses arrays instead of loads of bools and string variables:

static var talking = false; //not sure this should be static - at least there needs to be an instance version for it *this* script is talking var texts : String[]; //string array to be set in inspector private var currentTextIndex = -1; private var currentText = "";

function OnTriggerStay (other : Collider) { if(other.gameObject.tag == "Player") { if(Input.GetButtonUp("Interact")) { talking = true; ++currentTextIndex; if (currentTextIndex >= text.Length) { talking = false; currentText = ""; } else if (currentTextIndex > -1) currentText = texts[currentTextIndex]; } } }

function OnTriggerEnter(other : Collider) { if(other.gameObject.tag == "Player") { currentText = -1; } }

function OnGUI () { if (talking) { //show GUI Label using currentText as the string } }

Comment
Add comment · Show 2 · 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 Pengu · Jul 06, 2010 at 01:15 AM 0
Share

I do get what Tetrad & Eagle32 are saying and where my problem exactly lies. I also understand that I have to find a alternative way/solution myself otherwise I would never understand scripting. It seems I'm still lacking to much fundermental knowledge (+english is not my native language) so I don't really understand the 3rd answer $$anonymous$$ike posted. Well thanks for the quick replies - I will try my best

avatar image Mike 3 · Jul 06, 2010 at 01:20 AM 0
Share

Don't worry, you'll get there. I posted it mainly because I was 3/4 done when they'd posted, and for fun ;) I would take a look into arrays next though, they will help a lot with conversations

avatar image
1
Best Answer

Answer by Tetrad · Jul 06, 2010 at 12:17 AM

Input.GetButtonUp returns true the frame that key is pressed down.

So, in this case:

    if(Input.GetButtonUp("Interact"))
    {
        // stuff
        if(Input.GetButtonUp("Interact"))
        {
             // more stuff
        }
    }

Both if statements will return true the frame you release that button. So both "stuff" and "more stuff" will execute. You'll have to change your logic to something else.

What you'll probably want to do is have some kind of flag that says whether or not the text is printing out. If it is and the button is pressed up, skip to the end. If it isn't, go to the next one.

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
avatar image
1
Best Answer

Answer by Eagle32 · Jul 06, 2010 at 12:19 AM

From the GetButtonUp documentation

Returns true the first frame the user releases the virtual button identified by buttonName.

From your code

if(Input.GetButtonUp("Interact")) // <---------- { talking = true; TextA = true; print("TextA"); // console check

         if(Input.GetButtonUp("Interact"))  // &lt;----------
         {
             TextB = true;
             print("TextB"); // console check
         }
     }

Between the two indicated points the value returned by GetButtonUp does not change. You are still in the same frame. So you're always going to get the contents of both if statements executing at the same time.

That's your problem, I'll leave fixing it as an exercise for the reader.

[edit: beaten by 30 seconds]

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
avatar image
0
Best Answer

Answer by Pengu · Jul 15, 2010 at 01:07 AM

so I finally was able to find a way...

it's not the most elegant one and I still don't really know how to use arrays (I've tried and failed)

but here is how it worked for me:


static var talking = false; //(btw. static because I need to access this var from another script)

var page : int = 0;

var TextA = false; var TextB = false;

function OnTriggerStay (other : Collider) { if(other.gameObject.tag == "Player") { // Text 1 if(page == 0 && Input.GetButtonUp("Interact")) { talking = true;

         TextA = true;
         page = 1;
     }
     // Text 2
     else if(page == 1 &amp;&amp; Input.GetButtonUp("Interact"))
     {
         TextA = false;
         TextB = true;
         page = 2;
     }

// and so on...

function OnGUI () { if (talking) { if (TextA) { //GUI.Label A } else if (TextB) { //GUI.Label B }

// and so on...


so I used the "page" variable as a buffer to switch frames / as a flag to tell when to print or not, like you guys kinda implied me to do so.

thanks a lot everyone! :)

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 Elarith · Aug 25, 2012 at 01:13 AM 0
Share

If I could ask here, does that script run properly? I've tried to use a getButtonUp within an OnTriggerStay and the button is very unresponsive. It only works half the time at best. Have you encountered that issue before?

avatar image
0

Answer by Rotarus · Nov 18, 2011 at 11:36 AM

Pengu and others, thank you very much!

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

2 People are following this question.

avatar image avatar image

Related Questions

On Screen Button Controls 0 Answers

Change button texture when its clicked 1 Answer

Turn off my GUI Box when I click a button. 3 Answers

Cases if a GUI.Button is clicked two or more times?? 2 Answers

Making a Hover function for a button or toolbar from code. 2 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