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 /
  • Help Room /
This question was closed Sep 12, 2015 at 08:05 PM by Extrapafurdio for the following reason:

Problem is not reproducible or outdated

avatar image
0
Question by Extrapafurdio · Sep 10, 2015 at 12:51 AM · canvashierarchyactivation

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!

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 Positive7 · Sep 10, 2015 at 01:08 AM 0
Share

haveRead is always true

 if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.E)) 
         {
             haveRead = !haveRead;
             bigPage.SetActive (false);
         }
avatar image Extrapafurdio Positive7 · Sep 10, 2015 at 03:37 PM 0
Share

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.

avatar image Positive7 Extrapafurdio · Sep 10, 2015 at 04:08 PM 0
Share

change haveRead = !haveRead; to haveRead = false; Let me know if it doesn't work and I'll take a deeper look int this.

Show more comments
Show more comments
avatar image Extrapafurdio · Sep 10, 2015 at 05:49 PM 0
Share

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.

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

28 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Show Canvas when button is clicked, Unity 5.2.3 1 Answer

How can I organize menus as a tree hierarchy? 0 Answers

Getting component from derived classes 0 Answers

How to place objects in 2D game (hierarchy top layer / canvas lower layer?) 0 Answers

Unity Photon Pun 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