Problem is not reproducible or outdated
How to open and close a "message box"?
I have two scripts. One, attached to the player, in which, if it is standing on a page, and I press E, the game object is deactivated in the hierarchy. Then I have this other one...
public GameObject page;
public GameObject bigPage;
private bool haveRead = true;
void Update ()
{
if (!page.activeInHierarchy && haveRead)
{
bigPage.SetActive(true);
}
if (Input.GetKeyDown (KeyCode.E))
{
bigPage.SetActive (false);
}
}
...that is supposed to check if the page game object is deactivated in the hierarchy, and, if it is, it activates the bigPage canvas image, previously deactivated. If I press E, the canvas image should be deactivated again, but that's the problem. The canvas is activated properly, but right when I press E, it is immediately reactivated. I believe it is because the code is inside the void Update() function, but I have no idea how to "break this cycle" as the page will remain deactivated "forever". I appreciate any suggestion on how to solve this, or even on how to do it in a different way. Thank you very much!
haveRead is always true
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.E))
{
haveRead = !haveRead;
bigPage.SetActive (false);
}
This code works, but I wanted the bigPage to be activated immediately after the page is deactivated, and press E only to deactivate it, while not being able to open it again after.
change haveRead = !haveRead;
to haveRead = false;
Let me know if it doesn't work and I'll take a deeper look int this.
Ok, I just realized something. As I said, there are two scripts. What I want to do is, press E to collect the page (first script). Then, the bigPage appear, and when I press E again, it disappear again (second script). What I believe is that when I press E both things happen: the page is deactivated, activating the bigPage that is immediately deactivated, because E does BOTH things.
Here is the first script:
void OnTriggerStay2D(Collider2D page)
{
if (page.gameObject.CompareTag ("Collectible") && Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.E))
{
page.gameObject.SetActive (false);
bigPage.SetActive(true);
}
}
I also tried merging both scripts, but it is the same E dilemma:
public GameObject bigPage;
private bool haveRead = false;
void OnTriggerStay2D(Collider2D page)
{
if (page.gameObject.CompareTag ("Collectible") && Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.E))
{
page.gameObject.SetActive (false);
bigPage.SetActive(true);
}
}
void Update()
{
if (bigPage.activeInHierarchy)
{
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E) && haveRead == true)
{
bigPage.SetActive(false);
}
}
}
That works properly if I substitue the last if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E) && haveRead == true)
for if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.F))
.
So, I think that I need to create a "gap" between both actions? A way to separate the E for collecting and the E for deactivating the bigPage.