Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by OctoSloths · Nov 20, 2014 at 04:28 PM · javascriptinventorypauseguitext

Pause not working correctly

So I have this script:

 var firstPersonController:Transform;
 var lantern:GameObject;
 var paused : boolean = false;
 function Start () {
 inventoryText.guiText.text = "";
 }
 
 function Update () {
  if (Input.GetKeyDown("p")&& paused == true){
         inventoryText.guiText.text = "Lantern " + "[" + inventoryArray[0] + "]" + "\n"+ "Hit H to equip!";
         //inventoryArray[0]++;
         //inventoryArray[1] += 2;
         paused = true;
         if (Time.timeScale == 1.0)
             Time.timeScale = 0.0;
     }    
     if (paused == false){
     inventoryText.guiText.text = "";
     Time.timeScale = 1.0;
     }
      if (Input.GetKeyDown("h") && inventoryArray[0] > 0){
           var go = Instantiate(lantern) as GameObject;
           go.transform.parent = firstPersonController;
  
           inventoryArray[0] -= 1;
       }
       if (Input.GetKeyDown("p")){
         inventoryText.guiText.text = "Lantern " + "[" + inventoryArray[0] + "]" + "\n"+ "Hit H to equip!";
         //inventoryArray[0]++;
         //inventoryArray[1] += 2;
     }
 }

I want to be able to hit "p" and have the game pause as well as show the inventory, then when I hit "p" again I want it to not be paused and not show the inventory. However the script right now just blinks with the guitext shown when I hit "p" rather than pause the way I want it to.

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 MrSoad · Nov 20, 2014 at 04:02 PM 0
Share

Your code is a bit of a mess indentation wise, very hard to see what code belongs to what condition. Your logic seems to be equally messed up, you never set "paused = false" anywhere in the script you have given(only during your declaration at the top, which won't do anything again), so once it is true it will never become false again...

avatar image OctoSloths · Nov 20, 2014 at 05:16 PM 0
Share

Sorry for the messy script, I'm extremely new to program$$anonymous$$g so I'm not very good with this. :(

1 Reply

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

Answer by Anxo · Nov 20, 2014 at 04:39 PM

Add a return.

 if (Input.GetKeyDown("p")&& paused == true){
          inventoryText.guiText.text = "Lantern " + "[" + inventoryArray[0] + "]" + "\n"+ "Hit H to equip!";
          //inventoryArray[0]++;
          //inventoryArray[1] += 2;
          paused = true;
          if (Time.timeScale == 1.0)
              Time.timeScale = 0.0;
          return;  // <-- here
      }    

But better to handle things in outside methods, IMO so you can call it from other places like so. Forgive me, I write in C# not javascript but here we go

 var isPaused : bool = false;
 
 function Update (){
     if(input.GetKeyDown(KeyCode.P)){
       if(isPause){
          UnpauseGame();
       } 
       else{ 
          PauseGame();
       }
     isPaused = !isPaused;
     }
 }
 function PauseGame(){
   InvantoryText.enabled = true;
   Time.timeScale = 0;
 }
 
 function UnpauseGame(){
   invantoryText.enabled = false;
   Time.timeScale = 1;
 }
Comment
Add comment · Show 4 · 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 MrSoad · Nov 20, 2014 at 04:49 PM 0
Share

A couple of typos there "isPause" and "InvantoryText", also you always set isPaused to !isPaused so the initial problem still remains(it needs to switch in relation to the "if(isPause){" statement conditions above it rather than after this).

 function Update() {
 
     if (input.Get$$anonymous$$eyDown($$anonymous$$eyCode.P)) {
     
         if (isPaused) {
             isPaused = false;
             UnpauseGame();
             
         } else {
             isPaused = true;
             PauseGame();
         }
     }
 }
avatar image Anxo · Nov 20, 2014 at 05:06 PM 0
Share
 isPaused = !isPaused; /// is the same thing as 
 
 
 if(isPaused){
  isPaused = false;
 }else {
  isPaused = true;
 }
 
 // = )
 
 And yes, sorry for the types, I should have added the WUCC tag to the end of my answer. 
avatar image MrSoad · Nov 20, 2014 at 05:10 PM 0
Share

Ah, did not no that it worked that way(I always say true/false explicitly as I find it easier for me to read), cheers Anxo, +1 for you in return for today's lesson :)

avatar image OctoSloths · Nov 20, 2014 at 05:17 PM 0
Share

Thank you very much! $$anonymous$$y apologies for the messed up script in the first place.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Real Simple Inventory and Vendor System Help 1 Answer

I need some help on inventory. 0 Answers

How do I reference a UI text in Unity 5? (JS) 2 Answers

Where can i begin to learn how to make an inventory. (Javascript). 1 Answer

Inventory Help. 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