Search Unity

How To Pause Game On Collision?

Discussion in 'Scripting' started by Dogg, Jul 21, 2014.

  1. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    Hello, I want to know how can I pause the game(or the Player) right when I grab a power up, and then un-pause it after 3 seconds or so. I can't even get it to pause at all, even when i use Time.timeScale, or am I using it wrong? Please let me know. I've paused the game before, but that was when i pressed a GUI button for a pause menu. So it's different. Here's the script I'm trying to work with(it's the power up script):

    Code (CSharp):
    1. public GameObject coinEffect;
    2.  
    3.  
    4.    
    5.     void OnTriggerEnter2D(Collider2D other)
    6.     {
    7.                 if (other.tag == "Player") {
    8.                         Time.timeScale = 0f;
    9.                         ControllerScript playerScript = other.gameObject.GetComponent<ControllerScript> (); // not sure about the syntax here...
    10.                         if (playerScript) {
    11.  
    12.                              
    13.                                 playerScript.SetInvincible ();
    14.                                
    15.                            
    16.                                 playerScript.SpeedUp();
    17.  
    18.  
    19.                         }
    20.                        
    21.             GameObject effect = Instantiate(coinEffect, transform.position, transform.rotation) as GameObject;
    22.             Destroy(effect.gameObject, 3);
    23.             Destroy (gameObject);
    24.                 }
    25.         }
    26.    
    27.     }
    28.  
     
  2. zDemonhunter99

    zDemonhunter99

    Joined:
    Apr 23, 2014
    Posts:
    478
    From what I see, you're destroying the gameobject to which the script is attached to? This will cause all the components of the gameobject to be destroyed including the scripts.
     
  3. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    Ah I see thanks, so how can I pause the Player then? Do you have any suggestions?
     
  4. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    I'm still looking for a suggestion, if anyone is interested.
     
  5. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    I'm still looking for a suggestion, if anyone is interested in helping me. :(