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 /
avatar image
0
Question by mbhigham · Nov 03, 2016 at 03:18 PM · pausepause menupause gamepausegame

Pause Menu

Hi, my pause menu isn't working at all, I've followed someone's code to try and get it working and it just won't work at all, I hit pause and the menu pops up but I can still look about and move the camera around, what is wrong with my code?

I also get this error message "NullReferenceException: Object reference not set to an instance of an object PauseScript.Update () (at Assets/MyFolder/My Scripts/PauseScript.js:27)"

 #pragma strict
 
   var pauseCanvas : Canvas;
   var isPause = false;
 
 function Start()
 {
     Screen.lockCursor = true;
     Cursor.visible = false;
 }
 
 function Update()
 {
   if( Input.GetKeyDown(KeyCode.Escape))
   {
     isPause = !isPause;
     if(isPause) 
     {
     Time.timeScale = 0;
     pauseCanvas.enabled = true;
     }
     else 
     {
     Time.timeScale = 1;
     pauseCanvas.enabled = false;
     }
     (gameObject.Find("First Person Controller").GetComponent("MouseLook") as MonoBehaviour).enabled = false;
     (gameObject.Find("Main Camera").GetComponent("MouseLook") as MonoBehaviour).enabled = false;
     pauseCanvas.enabled = true;
     Time.timeScale = 0;
     Screen.lockCursor = false;
     Cursor.visible = true;
   }
 }
 
 function ResumeGame()
 {
 
 }
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

1 Reply

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

Answer by TBruce · Nov 03, 2016 at 04:33 PM

The reason why you get an error is because MouseLook is not a class derived from MonoBehaviour. This will fix you problem

 #pragma strict
 
 var pauseCanvas : Canvas;
 var isPause = false;
 
 function Start()
 {
     Screen.lockCursor = true;
     Cursor.visible = false;
     if (pauseCanvas != null)
     {
         pauseCanvas.gameObject.SetActive(false);
         pauseCanvas.enabled = false;
     }
 }
 
 function Update()
 {
     if( Input.GetKeyDown(KeyCode.Escape))
     {
         isPause = !isPause;
         if(isPause) 
         {
             Time.timeScale = 0;
             pauseCanvas.enabled = true;
             Screen.lockCursor = true;
             Cursor.visible = true;
         }
         else 
         {
             Time.timeScale = 1;
             pauseCanvas.enabled = false;
             Screen.lockCursor = false;
             Cursor.visible = true;
         }
 
         if (pauseCanvas != null)
         {
             if (GameObject.Find("First Person Controller") != null)
             {
                 GameObject.Find("First Person Controller").SetActive(!isPause);
             }
             pauseCanvas.enabled = isPause;
             pauseCanvas.gameObject.SetActive(isPause);
         }
     }
 }
 
 function ResumeGame()
 {
 }
Comment
Add comment · Show 8 · 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 mbhigham · Nov 03, 2016 at 05:42 PM 0
Share

That worked great! Thank you, my camera is still moving around when in the pause state? It shouldn't do that?

avatar image TBruce mbhigham · Nov 03, 2016 at 05:55 PM 0
Share

One sec and I will address that for you.

avatar image mbhigham TBruce · Nov 04, 2016 at 12:09 AM 0
Share

Do you think you can help me with something else?

http://answers.unity3d.com/questions/1266620/trigger-a-timer.html

avatar image TBruce mbhigham · Nov 03, 2016 at 06:20 PM 0
Share

Your original code had this

 Cursor.visible = false;

in the start menu. I just went with it. Here is an update to the script

 #pragma strict
 
 var pauseCanvas : Canvas;
 var isPause = false;
 var player : GameObject;
 
 function Start()
 {
     Screen.lockCursor = false;
     Cursor.visible = true;
     if (pauseCanvas != null)
     {
         pauseCanvas.gameObject.SetActive(false);
         pauseCanvas.enabled = false;
     }
 
     if (player == null)
     {
         if (GameObject.Find("First Person Controller") != null)
         {
             player = GameObject.Find("First Person Controller");
         }
         else if (GameObject.FindWithTag("Player") != null)
         {
             player = GameObject.FindWithTag("Player");
         }
     }
 }
 
 function Update()
 {
     if( Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Escape))
     {
         isPause = !isPause;
         if(isPause) 
         {
             Time.timeScale = 0;
             pauseCanvas.enabled = true;
             Screen.lockCursor = false;
             Cursor.visible = true;
         }
         else 
         {
             Time.timeScale = 1;
             pauseCanvas.enabled = false;
             Screen.lockCursor = false;
             Cursor.visible = true;
         }
 
         if (pauseCanvas != null)
         {
             if (player != null)
             {
                 player.SetActive(!isPause);
             }
             pauseCanvas.enabled = isPause;
             pauseCanvas.gameObject.SetActive(isPause);
         }
     }
 }
 
 function ResumeGame()
 {
 }

You should really place the script on a separate game object and definitely not on the FPS controller. Also, when you create the FPS controller using the prefab, the default name of the object is FPSController, but in your code you seem to have changed it to First Person Controller. You need to make sure that the name is correct both in the hierarchy and the script.

Also, you can set the tag of your player to Player (if not already done so and use GameObject.FindWithTag() ins$$anonymous$$d). For efficiency I created a player object variable and placed it as a global and set it in the Start() function ins$$anonymous$$d of contiually doing it.

avatar image mbhigham TBruce · Nov 03, 2016 at 07:21 PM 0
Share

Okay That all works fine, how can I keep a camera on when the game is paused?

Show more comments
avatar image mbhigham · Nov 03, 2016 at 05:45 PM 0
Share

And my cursor isn't working?

avatar image mbhigham · Nov 03, 2016 at 05:47 PM 0
Share

It just brings up my canvas and doesn't let me move the mouse around just spins the camera around.

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.

Follow this Question

Answers Answers and Comments

76 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 avatar image avatar image

Related Questions

How do i stop my mouse from being in first person when a pause menu appears? 0 Answers

How do I detect if an Admob Banner Ad has been tapped/opened? 1 Answer

Need to press 3 times to pause in-game in Unity 0 Answers

Pausing a game 2 Answers

Cannot move after switching scenes? 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