Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
3
Question by Steelwire · Oct 17, 2016 at 07:24 AM · c#scripting problempauseupdate function

Pause a script without pausing the whole game

Is it possible to pause a certain script? To let it repeat it's Update function as normal until a certain event happens (for example, a variable hits a certain value) and then freeze the script (but not the whole game) for an amount of time before letting it continue as normal again.

Comment
Add comment · Show 1
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 Bonfire-Boy · Oct 17, 2016 at 11:24 AM 1
Share

There are various ways you can achieve this kind of thing, some that spring to $$anonymous$$d are:

1) disable and then reenable the component

2) add your own flag that's checked in Update before it does what you want (this would allow you to pause some functionality but not other bits)

3) use a coroutine with an infinite loop, and kill the coroutine (either by explicitly stopping it or setting a flag that makes it break out) then restart it

2 Replies

· Add your reply
  • Sort: 
avatar image
6

Answer by nztree · Oct 17, 2016 at 11:59 AM

Well you can't "pause a script" but there are ways to achieve what you're looking for fairly simply.

For example, let's say you want a script on a gameobject to stop when a counter gets to 10.

 int value;
 void Update(){
 
    if (value >= 10)return;
    value++;
    //other stuff
 }

This will break your update loop once the value reaches 10, so anything else below will be 'paused'.

Alternatively, if you were looking for something to pause for a certain period of time, you could use a coroutine. For example:

 bool gameRunning;
 
 void Start(){
   gameRunning = true;
   StartCoroutine(doSomething());
 }
 
 IEnumerator doSomething(){
   while(gameRunning){
    //do something here
    yield return new WaitForSeconds(10);
   }
 }

This will run your loop in doSomething while gameRunning is true, but pause for 10 seconds each loop.

Hope this helps.

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 dadyno · Mar 08, 2018 at 11:47 AM 0
Share

$$anonymous$$aybe this code worked with previous versions but with newer versions it doesn't work. I've been trying to use this code and it gave me an error: error CS0119: Expression denotes a type', where a variable', value' or method group' was expected I've been trying to fix this for some time and I was looking for solutions in the Internet until I found out what you need to write "new" in front of "WaitForSeconds". And when I replaced

 yield return WaitForSeconds (addPauseTimeHere);

with

 yield return new WaitForSeconds (addPauseTimeHere);


it worked perfectly. Still thanks for the idea of coroutines!

avatar image nztree dadyno · Mar 08, 2018 at 06:41 PM 1
Share

Oh you're absolutely right. Just a mistake in the write up on my part. Will amend to fix.

avatar image
0

Answer by FirePlantGames · Oct 21, 2016 at 07:56 PM

So. You can do this many ways. If you just want to stop the Update from running, but keep some other functions running you can use a bool variable. Like so; public bool updateScript;

 void Update()
 {
        if (!updateScript) //If the UpdateScript variable isn't true 
            return; //Then we don't continue with the rest of our code
 }

Or. You could enable/disable the script by having one script control the state of the script you want running

 public Behaviour behaviourToEnableOrDisable; //The script you don't want to run at certain times
 
 void Update ()
 {
          if (Input.GetButtonDown("Fire1")) //if left mouse button is clicked
          {
                   behaviourToEnableOrDisable.enabled = !behaviourToEnableOrDisable.enabled //Toggle the script
          }
 }
 
Comment
Add comment · 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

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A Proper Way To Pause A Game 9 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Update within an if statement not working? 3 Answers

How to tell an operand "Once" or "Started to" 3 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