The if statement never executes when the key is pressed but the else always executes. I want the Scream to execute when the fox is active and the control key is pressed.
But if the fox is active and the key is not pressed then I want the score to change(AddScore).
If I comment out the else statement then the if works fine.
I have been working on this for a week and I have tried many different ways.
I hope someone can help me. Thanks
if (Input.GetKey(KeyCode.LeftControl) && foxActive)
{
Debug.Log("foxActive 1 = " + foxActive);
Scream.instance.CtrlScream();
foxActive = false;
}
else if (foxActive)
{
gameController.AddScore(stolenEggValue);
foxActive = false;
}
Your should work. Do you enable the boolean with code or in the editor? Since when you enable it in the editor it will not pick up you pressing a button. Just to confirm that your code works here’s the script I tested it with:
public bool foxActive;
private void Update()
{
if (Input.GetKey(KeyCode.LeftControl) && foxActive)
{
Debug.Log("foxActive 1 = " + foxActive);
// Scream.instance.CtrlScream();
foxActive = false;
StartCoroutine(timer());
}
else if (foxActive)
{
// gameController.AddScore(stolenEggValue);
foxActive = false;
Debug.Log("Only foxactive");
StartCoroutine(timer());
}
}
private IEnumerator timer()
{
yield return new WaitForSeconds(1);
foxActive = true;
}
Just start the game and hold CTRL and it should give you a log every second depending if you held the key or not.
Thanks for you help but it still doesn’t work properly. Yes if you hold the key down when the game starts it executes the if statement but if you don’t press the control key until the fox is active it executes the else first then the if. I have made many adjustment to your code but can’t get it to work;
The key press code will never be called in your example because you require both the key press AND foxActive to be true for it to be called. The problem is that if they don’t press the key and foxActive is true, it sets foxActive to false in your else statement. So basically on the first frame the script runs, it sets foxActive to false because the button wasn’t pressed and any future frames when it is pressed won’t meet the second requirement of foxActive being true.
Something like this might work for your needs:
public bool foxActive;
private void Update()
{
if (foxActive)
{
if (Input.GetKey(KeyCode.LeftControl))
{
Debug.Log("foxActive 1 = " + foxActive);
Scream.instance.CtrlScream();
foxActive = false;
}
else
{
gameController.AddScore(stolenEggValue);
}
}
}
Thanks everyone for your help. A minor adjustment to ChimerOs code almost works. I posted it below. If you hold down the Control key when you start the game then it the Scream will execute, but if you only try to press it when the fox appears (as intended) it doesn’t capture the key press.
foxActive is set in code in from another script at random times.
Baffeled!
public void Update()
{
if (foxActive)
{
if (Input.GetKey(KeyCode.LeftControl))
{
//Activate Player Scream at Fox
Debug.Log("foxActive 1 = " + foxActive);
Scream.instance.CtrlScream();
foxActive = false;
}
else
{
//Add to score if Player doesn't Scream at Fox
Debug.Log("foxActive 2 = " + foxActive);
gameController.AddScore(stolenEggValue);
foxActive = false;
}
}
}