• 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 /
avatar image
0
Question by repaj · Oct 14, 2015 at 09:39 AM · canvasinterfacescript loading

When I run a script which is attached to my canvas to pop up a pause menu it only runs once

Hi, I attached a script to my canvas called PauseMenu which runs once and pauses initially and resumes but never pauses again when clicking escape. Here is my code:

using UnityEngine; using System.Collections; using UnityEngine.UI; #if UNITY_EDITOR using UnityEditor; #endif using RTS;

public class PauseMenu : MonoBehaviour {

 Canvas canvas;
 private Player player;
 public Button Button2;


 void Start()
 {
     Debug.Log ("asdf");
     player = transform.root.GetComponent< Player >();
     canvas = GetComponent<Canvas>();
     canvas.enabled = false;
     ResourceManager.MenuOpen = false;
     Button2.GetComponent<Button>().onClick.AddListener(() => { Resume();});
 }
 
 void Update()
 {
     Debug.Log ("Jake");
     if(Input.GetKeyDown(KeyCode.Escape) && ResourceManager.MenuOpen == false) Pause();
     //Button2.GetComponent<Button>().onClick.AddListener(() => { Resume();});
     //if(Input.GetKeyDown(KeyCode.Escape)) Pause();
     //if(Input.GetKeyDown(KeyCode.Escape) && ResourceManager.MenuOpen == true) Resume();
 }

 public void Pause()
 {
     //if(Event.current.type == KeyCode.Escape)Debug.Log ("as12d12f2");
     //if(Event.current.type == EventType.KeyDown)Debug.Log ("as12d12f2");
     Debug.Log ("asdf2");
     canvas.enabled = true;
     Time.timeScale = 0.0f;
     if(player) player.GetComponent< UserInput >().enabled = false;
     //canvas.enabled = !canvas.enabled;
     //Time.timeScale = Time.timeScale == 0 ? 1 : 0;
     //ResourceManager.MenuOpen = true;
 }

 public void ResumePublic(){
     Resume ();
     }

 private void Resume() {
     Debug.Log ("asdf1");
     Time.timeScale = 1.0f;
     GetComponent< PauseMenu >().enabled = false;
     if(player) player.GetComponent< UserInput >().enabled = true;
     Cursor.visible = false;
     canvas.enabled = false;
     //Screen.showCursor = false;
     //GetComponent<UserInput > ().enabled = true;
     Debug.Log ("a2323sdf1");
     ResourceManager.MenuOpen = false;
     Button2.GetComponent<Button>().onClick.AddListener(() => { Resume();});
 }
 
 public void Quit()
 {
     #if UNITY_EDITOR 
     EditorApplication.isPlaying = false;
     #else 
     Application.Quit();
     #endif
 }

}

Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Unity Canvas Grid Layout - Keep cells perfect squares and fit in parent 0 Answers

Not correct ui scaling 0 Answers

How to display a canvas through multiple cameras + use camera culling mask with UI? 0 Answers

C# - Error " does not implement interface member" 2 Answers

Having text be a child to a game object? 2 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