Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Sep 10, 2015 at 03:04 AM by IHackedDeath for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by IHackedDeath · Aug 27, 2015 at 12:28 PM · c#menucontrolsfor loop

I am trying to create an in game controls menu so I can rebind controls in game, so I try a for loop in the new GUI system but it doesn't work?

So as the question states I am trying to create an in game controls menu to rebind controls for players, so far I have tried at least 10 different things to see if anything works and nothing so far.

I am using the GUI button to enter my function with the for loop which loops through 7 seconds and checks if the user pressed a button and then stores the button in a string to later be assigned, the problem is the GUI button calls the function just once.

my code is as follows:

     public bool playerOne = false;
     public bool playerTwo = false;
     public bool playerThree = false;
     public bool playerFour = false;
 
     public Text forward;
     public Text right;
     public Text left;
     public Text backward;
 
     bool isActive = false;
     string pressedKey;
 
     void Start () 
     {
         if (playerOne)
         {
             this.isActive = true;
         }
         if (playerTwo)
         {
             this.isActive = true;
         }
         if (playerThree)
         {
             this.isActive = true;
         }
         if (playerFour)
         {
             this.isActive = true;
         }
     }
 
     public void forwardControl ()
     {
         if (this.isActive)
         {
             PassKey (this.forward.text);
         }
     }
 
     public void rightControl ()
     {
         if (this.isActive)
         {
             for (int i = 0; i < 10; i++)
             {
                 if (Input.anyKeyDown && !Input.GetMouseButton(0) && !Input.GetMouseButton(1) && !Input.GetMouseButton(2))
                 {
                     pressedKey = Input.inputString;
                 }
             }
             this.right.text = pressedKey;
             pressedKey = null;
         }
     }
 
     void PassKey (string key)
     {
         for (int i = 0; i < 7; i++)
         {
             Debug.Log(7 - i + "... please enter a key");
             if (Input.anyKeyDown && !Input.GetMouseButton(0) && !Input.GetMouseButton(1) && !Input.GetMouseButton(2));
             {
                 pressedKey = Input.inputString;
                 key = pressedKey;
                 i = 7;
             }
         }
     }
 }

for the forwardcontrol function i use the passkey function to try the for loop and assigning pressedkey but it stops after the first loop and prints (7 ... please enter a key)

The other function is an earlier attempt at solving it which failed and slight moderations to the code caused it to crash in an infinte loop or take a good 10 seconds going through the for loop.

What do I need to do in order for this to work.

p.s. I only code in c# but I understand javascript if you only know javascript.

Comment
Add comment · Show 6
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 IHackedDeath · Sep 01, 2015 at 01:28 PM 0
Share

Hey Guys,

I really need help with this question, I have tried really hard to answer this on my own but with no success and can't find anywhere that might be able to assist without rewriting a button myself and going about it another.

Can someone please let me know whether this is at all possible or if I actually have to use a whole new method.

$$anonymous$$ind Regards,

IHackedDeath.

avatar image getyour411 · Sep 03, 2015 at 04:35 AM 0
Share

I just took a cursory look at this and don't have a full solution/code but based on what you are describing it sounds like a good candidate for a c# Coroutine which is a really good tool to do something with a time-based approach such as

 coroutine
 timer=7
 keypress=false
 while(timer >0) {
 see if a button is pressed... if so (keypress == true, timer=0) else
 timer--
 yield return waitforseconds(1)
 if(keypress) do someaction based on that

avatar image IHackedDeath · Sep 03, 2015 at 01:20 PM 0
Share

Hey getyour411,

Thank you for taking time to help me with this problem.

I have tried coroutines before but they havn't worked.

I will try yours though and hope for some breakthrough, will let you know how it goes.

avatar image IHackedDeath · Sep 03, 2015 at 02:12 PM 0
Share

I have tried the coroutine again and nothing has happened again unfortunately.

here is my code if I am writing something wrong.

 public void forwardControl ()
     {
         keyPass (this.forward.text);
     }
 
 IEnumerator keyPass (string key) 
     {
         float timer = 7;
         bool keypress = false;
         while(timer > 0) 
         {
             if (Input.any$$anonymous$$eyDown && !Input.Get$$anonymous$$ouseButton(0) && !Input.Get$$anonymous$$ouseButton(1) && !Input.Get$$anonymous$$ouseButton(2));
             {
                 pressed$$anonymous$$ey = Input.inputString;
                 keypress = true;
             }
             timer--;
             yield return new WaitForSeconds(1);
             if(keypress)
             {
                 key = pressed$$anonymous$$ey;
                 timer = 0;
             }
         }
     }
avatar image getyour411 · Sep 03, 2015 at 05:44 PM 0
Share

You didn't call it as a coroutine, ie StartCoroutine(thename());

Show more comments

2 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by IHackedDeath · Sep 04, 2015 at 12:18 PM

Hi guys,

Thank you all for attempting to answer this question for me.

I have attempted all theories and possible solutions, but none of them have worked.

I have just done a simple yes or no bool for the button which then triggers a function through the update function so it is always looking for the bool to be true, also using the update function allows me to use time which GUI doesn't let me use time for some weird reason (still happy to accept an answer for why GUI Button doesn't use time).

Thank you all again for trying to help I appreciate it :)

Kind Regards,

IHackedDeath.

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

Answer by RedHedZed · Sep 01, 2015 at 04:18 PM

I assume i = 7 is to make the for loop terminate early? Could you not use a break instead?

Additionally, this for loop won't run for 7 seconds. It runs the loop 7 times, sure, but this runs much faster than 7 seconds.

you could try something more like this if you want it to run for 7 seconds:

 float timeRemaining = 7.0f
 while (timeRemaining > 0){
      timeRemaining -= Time.deltaTime;
      if (Input.anyKeyDown && !Input.GetMouseButton(0) && !Input.GetMouseButton(1) && !Input.GetMouseButton(2)){
            pressedKey = Input.inputString;
            key = pressedKey;
            timeRemaining = 0;
      }
 }

Comment
Add comment · Show 5 · 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 IHackedDeath · Sep 02, 2015 at 12:40 PM 0
Share

Hi RedHedZed,

Thank you so much for taking the time to help me out,

I have tried your solution and unfortunately It does the same thing.

Once the button has been pressed it immediately cancels out of the function and doesn't wait for the 7 seconds.

Is this not possible with buttons? do they only play at the current frame they have been pressed?

Also if I hard write a new script for the button will that work, or even if I use the event trigger will that work?

Thanks in advance for any advice.

$$anonymous$$ind Regards,

IHackedDeath.

avatar image pmaloo · Sep 02, 2015 at 01:08 PM 0
Share

@IHackedDeath remove the timeRemaining = 0;from @RedHedZed script to make it run for full 7 seconds.

avatar image RedHedZed · Sep 02, 2015 at 02:22 PM 0
Share

@IHackedDeath I'm so sorry! I thought you wanted it to run for 7 seconds, or until a key was hit. $$anonymous$$y bad!

@pmaloo is right. Remove the timeRemaining = 0 to make it run the full 7 seconds.

avatar image IHackedDeath · Sep 03, 2015 at 03:21 AM 0
Share

Hey pmaloo,

Thanks for continuing to help guys, unfortunately it is still not recognizing the while loop and will enter the function once and stop after I have pressed the button.

Also I was after it checking for 7 seconds unitl the user has pressed a button, but I am not fussed with how this is done, just as long as i can enter a key when I press the button.

avatar image IHackedDeath · Sep 04, 2015 at 02:27 AM 0
Share

Hey guys, thanks again for all your help.

I have just realized that I had 4 copies of the script so I am getting rid of three and it should be working now.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to find the minimum number in a float array C# 1 Answer

UNITY 2D: How to make a level selection menu (like the video in details)? 1 Answer

C sharp menu problem with bottons 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