Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 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
0
Question by importguru88 · Sep 10, 2016 at 10:59 PM · gameobjectpausehowunpause

How do I make the gameobject pause and unpause on its own in unity3d

I need to make the gameobject pause and unpause by itself in the scene . I was able to pause and unpause the gameobject by key. I need the gameobject to be pause for a couple of seconds and unpause on its own . Here is my script :

 using UnityEngine;
 using System.Collections;
 
 public class hu : MonoBehaviour {
 GameObject[] pauseObjects;
     // Use this for initialization
     void Start () {
     Time.timeScale = 1;
     pauseObjects = GameObject.FindGameObjectsWithTag("Player");
 
     }
     
     // Update is called once per frame
     void Update () {
     
         {
             if(Time.timeScale == 8)
             {
                 Time.timeScale = 0;
                 
             } else if (Time.timeScale == 0){
             
                 Time.timeScale = 1;
                 
             }
     }
 }
 }
Comment
Add comment · Show 3
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 Sergio7888 · Sep 10, 2016 at 11:37 PM 0
Share

Time.timeScale = 0 will pause all your game.

avatar image importguru88 Sergio7888 · Sep 11, 2016 at 12:00 AM 0
Share

I change if(Time.timeScale == 8) to if(Time.timeScale == 0) { {

How will I make it unpause ?

avatar image importguru88 Sergio7888 · Sep 11, 2016 at 12:30 AM 0
Share

It will not pause at all.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by tkati · Sep 11, 2016 at 02:09 AM

You'll want to look into coroutines and then also need another manager to watch when to turn the object back on . The learning tutorials have several good examples, don't have to watch tutorials you should be able to look for things like IEnumerator to get to the sections of the displayed code that you want, probably even a tutorial specifically on corroutines . Probably also want to look into enable and disable of object the roller 2d examples with the sphere has some good examples.

here is some code example with a coroutine: public class ScrGameController : MonoBehaviour {

 public float startWaitSeconds;
 public float waveWaitSeconds;
 public float spawnAsteroidsWaitSeconds;
 public int asteroidHazardCt;
 public GameObject [] gameHazards;
 public GameObject boundaryRef;
 public Vector3 spawnValues;
 public GUIText txtReadySetGo;
 public GUIText txtScore;
 public GUIText txtRestart;

 private int playerScore;

 private bool exitGame;
 private bool restartGame;
 private bool continueGame;

 IEnumerator SpawnWaves(){
     //txtReadySetGo.GetComponent<GUIText>().text = "Ready ! ! !";
     txtReadySetGo.text = "Ready ! ! !";
     yield return new WaitForSeconds(startWaitSeconds);
     //txtReadySetGo.GetComponent<GUIText>().text = "Set ! ! ! !";
     txtReadySetGo.text = "Set ! ! ! !";
     yield return new WaitForSeconds(startWaitSeconds);
     //txtReadySetGo.GetComponent<GUIText>().text = "Go ! ! ! ! !";
     txtReadySetGo.text = "Go ! ! ! ! !";
     yield return new WaitForSeconds(startWaitSeconds-0.5f);
     //txtReadySetGo.GetComponent<GUIText>().text = "";
     txtReadySetGo.text = "";

     while (true){
         for (int i = 0; i < asteroidHazardCt; i++)
         {
             GameObject hazard = gameHazards[Random.Range(0, gameHazards.Length)];
             Instantiate(hazard, new Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z), Quaternion.identity);
             yield return new WaitForSeconds(spawnAsteroidsWaitSeconds);
         }

         if (!continueGame)
         {
             break;
         }

         yield return new WaitForSeconds(Random.Range(0.5f,waveWaitSeconds));
     }

     if (!continueGame)
         restartGame = true;
 }

 // Use this for initialization
 void Start () {

     //initialize game variables
     startWaitSeconds = 1.0f;
     waveWaitSeconds = 3.0f;
     spawnAsteroidsWaitSeconds = 0.5f;
     asteroidHazardCt = 6;

     continueGame = true;
     restartGame = false;
     exitGame = false;

     playerScore = 0;

     txtReadySetGo.text = "";
     txtScore.text = "";
     txtRestart.text = "";

     UpdateScoreDisplay();

     StartCoroutine(SpawnWaves());
 }
 
 // Update is called once per frame
 void Update () {
     if (restartGame == true)
     {
         if (Input.GetKeyDown(KeyCode.R))
         {
             //depracated is Application.LoadLevel(Application.LoadedLevel)

             restartGame = false;
             continueGame = true;

             ReplayLevel01();
         }
         else {
             if (Input.GetKeyDown(KeyCode.E))
             {
                 Application.Quit();
             }
         }
     }
 }


 public void GameOver()
 {
     txtReadySetGo.GetComponent<GUIText>().text = " Game Over ";
     continueGame = false;
     txtRestart.text = "Restart Game Press \'R\'\nPress \'E\' to Exit.";
 }

 public void AddScore(int addedScore)
 {
     playerScore += addedScore;
     UpdateScoreDisplay();

 }

 public void UpdateScoreDisplay()
 {
     txtScore.text = "Score: " + playerScore;
 }

 public void ReplayLevel01()
 {
     //calls the active scene
     string sceneName = SceneManager.GetActiveScene().name;

     //loads the active scene
     SceneManager.LoadScene(sceneName, LoadSceneMode.Single);
 }

}

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

69 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

Related Questions

Gameobject.SetActive not working on Android? 1 Answer

In game pause menu 2 Answers

How do I start game paused 2 Answers

how to make your game object auto scale in any resolution 0 Answers

How to unpause an audiosource after pausing it in a previous scene? 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