• 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 /
avatar image
1
Question by Xark96 · Sep 08, 2017 at 10:13 AM · scripting beginner

How to disable a canvas while another is opened?

Hi everyone!

I'm a coding noob and can't find a proper solution on the web either..

My problem: I've got two scripts on seperate objects.

One script triggers the Pausemenu.

The other script triggers the Equipmentmenu.

I can't get it done that if the Pausemenu is open to disable the Equipmentmenu script So while in pause menu I still can open and close the equipmentmenu :/

My Pause script(the other script is nearly the same):

 using System.Collections;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class PauseGame : MonoBehaviour
 {
 
  public Transform canvas;
  
  
  // Update is called once per frame
  void Update ()
 {
 
      if (Input.GetKeyDown(KeyCode.Escape))
      {
          Pause();
      }
 }
 
 public void Pause()
 
  {
      if (canvas.gameObject.activeInHierarchy == false)
      {
          canvas.gameObject.SetActive(true);
          Time.timeScale = 0;
      }
      else
      {
          canvas.gameObject.SetActive(false);
          Time.timeScale = 1;
      }
  }
  
  public void Quit()
  {
      Application.Quit();
  }
 }

Comment
Add comment · Show 2
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 08, 2017 at 10:30 AM 0
Share

In your other script

  public GameObject Pause$$anonymous$$enu;
 
     private void Update()
     {
          if(Pause$$anonymous$$enu.activeSelf) return;
     
          //Do something
     
     }
avatar image N00MKRAD · Sep 08, 2017 at 12:04 PM 0
Share

Why do you have a Transform variable for the Canvas?

GameObject is better since you don't need the ".gameObject" anymore.

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Casy10 · Sep 08, 2017 at 11:14 AM

Hello @Xark96 ! If i understood correctly, a solution would be to use a Manager.cs script. Place it on an empty game object that will be always active in scene. Try to handle the user input in that script and keep references to objects that you're working with(i.e. the objects with the above-mentioned scripts). If the requested key is pressed then deactivate the entire object and be sure that the other object is active otherwise activate it.

     public class Manager : MonoBehaviour
     {
     public GameObject FirstCanvas;
     public GameObject SecondCanvas;
     
     public void Pause()
     
         {
             if (FirstCanvas.gameObject.activeInHierarchy == false)
             {
                 FirstCanvas.gameObject.SetActive(true);
                 SecondCanvas.gameObject.SetActive(false);
                 Time.timeScale = 0;
             }
             else
             {
                 FirstCanvas.gameObject.SetActive(false);
                 SecondCanvas.gameObject.SetActive(true);
                 Time.timeScale = 1;
             }
         }
 }

If this won't help you please take your time and ask the question carefully.

Also, check this link : http://answers.unity3d.com/questions/779861/what-is-the-correct-way-to-switch-ui-panels-46.html

Comment
Add comment · Show 2 · Share
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 Xark96 · Sep 08, 2017 at 12:42 PM 0
Share

Even thoug your code didn't changed anything for some reason, it actually helped me thinking from a different angle and I found a solution. I simply added "public GameObject Equip;" to the PauseScript and put the Equip$$anonymous$$enu gameobject in the editor. Then I did actually the same as you did here but only to the new added Gameobject. It was as simple as that. Needed only 3 more lines of code ^^'

Like this:

public GameObject Equip;

pulic void Pause() { if (canvas.gameObject.activeInHierarchy == false) { canvas.gameObject.SetActive(true); Equip.gameObject.SetActive(false); Time.timeScale = 0; } else { canvas.gameObject.SetActive(false); Equip.gameObject.SetActive(true); Time.timeScale = 1; } }

avatar image Casy10 · Sep 08, 2017 at 01:05 PM 0
Share

Well thought. This way you will have your script on one of the 2 objects in the hierarchy. It seems that I didn't understand your question entirely but I'm glad the answer at least helped you.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

74 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 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 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

Melee & range ai 0 Answers

How to move an airplane in the Z axis? 0 Answers

Getting an error while trying to get scene name 1 Answer

How to make a collision script 1 Answer

GetAxis to GetTouch 0 Answers


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