- Home /
The question is answered, right answer was accepted
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.
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.
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
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.
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;
}
}
}
You didn't call it as a coroutine, ie StartCoroutine(thename());
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.
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;
}
}
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.
@IHackedDeath remove the timeRemaining = 0;
from @RedHedZed script to make it run for full 7 seconds.
@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.
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.
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.