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 Rick74 · Dec 17, 2013 at 08:44 PM · javascriptpausegame

Trying to pause a script...

Hey guys!

So I've created a pause function for my game within a clock script. And now I have every object or asset that I want paused outside of the Time.Scale feature to access this script.

Everything I've wanted to pause so far, will pause...except this script. I for the life of me cannot get this sucker to pause and resume on command.

 #pragma strict
 
 import SmoothMoves;
 
 var resourceValue:            int     =     25;
 
 var Countdown:                int     =     5;
 var visible:                 float     =     0.50;
 var invisible:                float     =     0.50;
 var blinkFor:                 float    =     5.0;
 
 var myAnimation:             SmoothMoves.BoneAnimation;
 
 var getPlayTime:        float     =     0.0;
 private var spawn:                boolean =     true;
 private var startState:         boolean =    true;
 private var startBlink:            boolean =     true;
 var getPlayTimeEnabled: boolean =     true;
 private var getClock:            GameObject;
 var whenAreWeDone:        float     =     0.0;
 
 function Start () 
 {    
     getClock = GameObject.FindGameObjectWithTag("clock");    
     
 }
 
 function Update ()
 {
     getPlayTime = getClock.GetComponent(clock).playTime;
     getPlayTimeEnabled = getClock.GetComponent(clock).playTimeEnabled;
     
     
     if (getPlayTimeEnabled)
     {
         myAnimation["Idle"].speed         = 1;
         myAnimation["Spawn"].speed         = 1;
         
         //renderer.enabled = true;
     
     }
     else
     {
         myAnimation["Idle"].speed         = 0;
         myAnimation["Spawn"].speed         = 0;
         //renderer.enabled = false;
         
     }
         
     if (startBlink && getPlayTimeEnabled)
     {
         Blink ();
         startBlink = false;
     }
     
 }
 
 function Spawn ()
 {
     animation.Play("Spawn");
     yield WaitForSeconds (1);
     startBlink = true;
 }
 
 function Blink ()
 {
     whenAreWeDone = getPlayTime + blinkFor;
     
     animation.Play("Spawn");
     yield WaitForSeconds (1);
     animation.Play("Idle");
     renderer.enabled = startState;
     yield WaitForSeconds(Countdown);
     
     if (getPlayTimeEnabled)
     {
         while (getPlayTime < whenAreWeDone)
         {
             if ( startState )
             {
                 renderer.enabled = false;
                 yield WaitForSeconds(invisible);
                 renderer.enabled = true;
                 yield WaitForSeconds(visible);
             } 
             else
             {
                 print ("in else");
                 renderer.enabled = true;
                     yield WaitForSeconds(visible);
                 renderer.enabled = false;
                 yield WaitForSeconds(invisible);
             }
         }
     
     renderer.enabled = true;
     Destroy ( gameObject );
        }
 }

It's basically an object that, after it is instantiated, will flash after some time...then destroy itself.

The problem I'm having is, I cannot get it to pause during the flash. (when the code is in the while statement) As you can see I have a global boolean that would detect if the game has been paused, "playTimeEnabled". And a float that represents the game's Time.time. "playTime".

If anyone has any ideas, I'm open to suggestions.

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
0

Answer by iwaldrop · Dec 18, 2013 at 07:09 AM

It looks like you should be checking the 'getPlayTimeEnabled' flag inside of your while loop, but your variable names are nearly meaningless to anyone but yourself, so it's hard to say for sure.

You have some weird things going on. Why not just cache a reference to the Clock (or better yet use a singleton pattern on the class or some static methods) instead of using two GetComponent calls each frame per game object that uses this method of getting those variables? That's what I'd do. For instance:

 if (Clock.enabled)
 {
     // do something
 }

is way better than:

 var isClockEnabled : boolean;
 
 void Update()
 {
     var clock = getClock.GetComponent(clock);
     isClockEnabled = clock.enabled;
 }
 
 void SomeMethod()
 {
     if (isClockEnabled)
     {
         // do something
     }
 }

Also notice that my variable names are meaningful in and of themselves so that anyone reading the code can figure out exactly what they mean. Self-documenting code is the best kind, my friend.

Comment
Add comment · Show 3 · 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 iwaldrop · Dec 18, 2013 at 07:29 AM 0
Share

As an example, here's a quick and dirty clock that should allow you easy access to the clock's info. It should work with your JavaScript stuff if you set it higher in the the script compilation order, but it's really just an example. You can get, and store, a reference to it anywhere you want, but you can also access it's values without a reference.

 using UnityEngine;
 
 public class Clock : $$anonymous$$onoBehaviour
 {
     public float timeScale;
     public float time;
 
     private static Clock instance;
     public static Clock Instance
     {
         get
         {
             if (instance == null)
             {
                 instance = GameObject.FindObjectOfType<Clock>();
                 GameObject clockGO = new GameObject("clock", typeof(Clock));
                 instance = clockGO.GetComponent<Clock>();
             }
             return instance;
         }
     }
 
     public static float Time
     {
         get
         {
             return Instance.time;
         }
     }
 
     public static float TimeScale
     {
         get
         {
             return Instance.timeScale;
         }
 
         set
         {
             Instance.timeScale = value;
         }
     }
 
     void Update()
     {
         time += UnityEngine.Time.deltaTime * timeScale;
     }
 }
 
avatar image Rick74 · Dec 18, 2013 at 09:43 PM 0
Share

Hey thanks for taking the time out to help me norm! ( pun intended) my entire pause method is probably a little sloppy. I'm an artist at heart and only started coding 5 months ago.

I was curious if you could more thoroughly explain what you mean by setting a script higher in the compilation order and why that makes it easier for other scripts to access it?

avatar image iwaldrop · Dec 19, 2013 at 05:00 AM 0
Share

Definitely. The docs are here. Essentially, in order for a UnityScript file to access variables in a C# file, you need to ensure that the C# stuff is in a particular place in your project. This works both ways, however, hence why I referenced the Script Compile Order page that Unity provides. Let me know if you need any additional help.

One thing I left out of the example script above is deltaTime. Which can be gotten by tracking the last time, and returning the difference of the last time and the current time.

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

18 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

Related Questions

Setting Scroll View Width GUILayout 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Way to pause/unpause, or stop/start mouselook script? 2 Answers

Iphone Unity and Best book . 1 Answer

OnTriggerEnter getting delay when object is moving? 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