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 /
This question was closed May 23, 2014 at 06:16 AM by Fattie for the following reason:

Duplicate Question

avatar image
0
Question by BenDurch · May 23, 2014 at 01:28 AM · timetimescale

I Need Help converting somthing to C#

i am rewriting my pause menu script in C# and i am having a bit of a problem with freezing the game. it sounds very simple at first, but my game does not contain a constant time scale, the time scale changes. this worked perfectly in js., but it just won't do the same in C#

 void OnGUI(){
 if (GUI.Button(new Rect(Screen.width/2,Screen.height/2,50,30),"Button")){
 Active = !Active;
 }
 }
 void Update(){
 
 _previousTimeScale = (Time.timeScale != 0) ? (Time.timeScale) : (_previousTimeScale);
 
         if (Active == true){ 
             Time.timeScale = 0; 
         }else{ 
             Time.timeScale = _previousTimeScale; 
         }
 }
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 rutter · May 23, 2014 at 01:17 AM 0
Share

What's the problem, exactly? Is there an error message? Is something happening that's not supposed to? Could you give us more information to work with?

avatar image Fattie · May 23, 2014 at 06:16 AM 0
Share

don't use update. search for 1000,000,000 questions on this

3 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by Kiwasi · May 23, 2014 at 03:03 AM

Remove the whole thing from update and add a pause method. Removing from update will improve efficiency

 public static bool isActive;
 private static float previousTimeScale
 
 void OnGUI () {
     if (GUI.Button(new Rect(Screen.width/2,Screen.height/2,50,30),"Button")){
         Pause();
     }
 }
 
 public static void Pause () {
     isActive = !isActive;
     if(!isActive){
         previousTimeScale = Time.TimeScale;
         Time.TimeScale = 0;
     } else {
         Time.TimeScale = previousTimeScale;
     }
 }

Making this all static will allow you to pause your game from anywhere simply by calling the Pause() method. Making isActive static will let you check if the game is paused from anywhere. I would suggest changing the name to isPaused for clarity.

Comment
Add comment · Show 1 · 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 BenDurch · May 23, 2014 at 01:01 PM 0
Share

never $$anonymous$$d, i got it now...

     if(GUI.Button(new Rect(Screen.width - 100, 10, 90, 90), PausePic)){
         Active = !Active;
     }

     _previousTimeScale = (Time.timeScale != 0) ? (Time.timeScale) : (_previousTimeScale);
     
     if (Active == true){ 
         Time.timeScale = 0; 
     }else{ 
         Time.timeScale = _previousTimeScale; 
     }
avatar image
-1

Answer by PippyLongbeard · May 23, 2014 at 02:03 AM

Personally, I would do this by assigning the '_previousTimeScale' variable to the 'Time.timeScale' property at the beginning, like so:

 int _previousTimeScale;
 
 void Start 
 {
      _previousTimeScale = Time.timeScale;
 }

This will make a solid base time scale at the beginning to reference from. There's no need to use a conditional operator that keeps reassigning the same value over and over. I haven't tested this, but that should fix it. Hope this helps!

Comment
Add comment · Show 1 · 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 BenDurch · May 23, 2014 at 12:36 PM 0
Share

that would not work for what i am doin

avatar image
0

Answer by Tarlius · May 23, 2014 at 05:06 AM

If you are getting different behaviour after converting to c#, it would be a good idea to provide the original js as well as saying what the problem/difference actually is.

Is this your whole source file? My personal experience with java is somewhat limited, but I seem to remember that java doesn't require you to actually declare the class (or anything really... Use #pragma strict always!). Did you wrap the code in:

 using UnityEngine;
 public class PauseClass : MonoBehaviour { // Note the class name must match the .cs filename
     // your code here
 }

Also, BoredMormon's suggestion about refactoring to not use update is a good one. If you really need to track timescale that much, it might be a good idea to write a wrapper for updating time (although if it is every frame then your approach is ok).

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 BenDurch · May 23, 2014 at 12:42 PM 0
Share

actually, the script is over 1000 lines because it is a pause menu with pages within pages. the whole thing is weaved to a public class called resources where i serialize and save the players armor, money, and progress. the problem i am having is that the pause thing does not want to work. my player encounters orbs called atoms that alter the players size or time scale. thats why i needed one that alters time.

avatar image Tarlius · May 23, 2014 at 03:16 PM 0
Share

Over 1k lines? o.o

Sounds like refactoring time! ;)

Follow this Question

Answers Answers and Comments

25 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

Related Questions

How to to do a progress bar wich scale down in 6 seconds ? (timeScale used) 0 Answers

Can't really pause my game 1 Answer

Unity how to find load time? 1 Answer

Unity Time.timeScale is out of range. Any chance to set it to 360? 1 Answer

2nd independent deltaTime and timeScale variables, or a way to mimic this? 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