How to use the same input for two different actions without them happening simultaneously.
This is a simple problem, but I just can't seem to find a solution: what I want is, when I press, to deactivate the page, to instantaneously activate the bigPage, then, when I press E again, the bigPage is deactivated. But, actually, when E is pressed, everything happens at the same time: page deactivates, bigPage activates and deactivates.
So I wish to find a way to separete these actions so they don't happen simultaneously, still using E as input for both. This is the code.
public GameObject bigPage;
private bool haveRead = false;
void OnTriggerStay2D(Collider2D page)
{
if (page.gameObject.CompareTag ("Collectible"))
{
if (Input.GetKeyDown(KeyCode.E) && haveRead == false)
{
page.gameObject.SetActive(false);
bigPage.gameObject.SetActive(true);
haveRead = true;
}
if (Input.GetKeyDown(KeyCode.E) && haveRead == true)
{
bigPage.SetActive(false);
haveRead = false;
}
}
}
Note that I tried putting some booleans, but they don't do anything (because their values are also changed simultaneously).
If I change any if (Input.GetKeyDown(KeyCode.E))
to any other key, it works properly. But I want both to be E.
Well, the second "if" is always going to happen after the first one does because you set haveRead to true inside the first "if", which will then mean it's true in the second "if" check, thus triggering it. Try replacing it with else if.
Remember that this is all happening within the same frame.
Answer by Rostam24 · Sep 12, 2015 at 08:37 PM
Try again with:
else if (Input.GetKeyDown(KeyCode.E) && haveRead == true)
{
bigPage.SetActive(false);
haveRead = false;
}
It doesn't work, because I think the problem remains: when I press E, the page is deactivated, and the bigPage is activated and deactivated.
One more try then:
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E)
{
if(haveRead) *edit*: this should be: (!haveRead)
{
page.gameObject.SetActive(false);
bigPage.gameObject.SetActive(true);
haveRead = true;
}
else
{
bigPage.SetActive(false);
haveRead = false;
}
}
Still doesn't. Now I think that the bigPage is immediately deactivated because if any other thing happens, it will get deactivated, and many other things happen. >:
Answer by getyour411 · Sep 12, 2015 at 08:38 PM
if(key)
if(haveRead == false)
// do stuff
ELSE // which means haveRead is true)
// do the other stuff
The problem with the way you have it written is that when it's false it does stuff, sets haveRead to true, then moves to the second if block and finds haveReads is true so it does stuff again (and again...)
Yup, that's exactly what I have written, and don't know how to get out of it. I'm sorry, but I didn't really understand your code. >:
Answer by Tatanchis · Jun 04, 2018 at 09:38 PM
I know it is late to answer this question but i was looking for the same and i found myself this solution:
private int number = 1;
void Update(){
if (Input.GetKeyUp(KeyCode.Z) && (number%2) == 1){
//Your actions here
number = number + 1;
}else if (Input.GetKeyUp(KeyCode.Z) && (number % 2) == 0)
{
//Your other actions here
number = number - 1;
}
}
As there are only two possible conditions we just see if the number is odd or not and modify the number depending on the condition. I hope this helps someone, cheers :)
Answer by kami1339 · Feb 02, 2019 at 05:09 PM
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class EnableDisableOtherscript : MonoBehaviour { public GameObject cam1; public GameObject cam2; private bool haveRead = false; // Use this for initialization
void Start ()
{
}
void Update ()
{
if (Input.GetKeyDown(KeyCode.E) && haveRead == false)
{
cam1.SetActive(false);
cam2.SetActive(true);
haveRead = true;
return;
}
if (Input.GetKeyDown(KeyCode.E) && haveRead ==true)
{
cam1.SetActive(true);
cam2.SetActive(false);
haveRead =false;
}
}
}