Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Pause button Android

Discussion in 'Scripting' started by GameForLife, Jan 12, 2014.

  1. GameForLife

    GameForLife

    Joined:
    Aug 17, 2013
    Posts:
    41
    I am having problems with my pause button. I have the GUITexture and varibles set up properly. However, when I press the button on the phone or tablet, I have to hold it to pause the game. I would like to press the button to stop the game and then press it again to start the game.


    Code (csharp):
    1.  
    2. var pauseButton : GUITexture;
    3.  
    4.  
    5. function Update()
    6. {
    7.  
    8. for(var touch : Touch in Input.touches)
    9.  
    10. if(touch.phase==TouchPhase.Began  guiTexture.HitTest(touch.position))
    11.     {
    12. //          if(GUI.Button(Rect(200,0,200,39),"Pause Button" ))
    13.  
    14.         doPauseToggle();
    15.     }
    16.  
    17.     else if(touch.phase==TouchPhase.Ended  guiTexture.HitTest)
    18.     {
    19.         unPauseGame();
    20.     }
    21.  
    22. //  GUI.matrix = Matrix4x4.TRS (Vector3.zero, Quaternion.identity, Vector3(Screen.width / 600.0, Screen.height / 450.0, 1));
    23.  
    24.     // Draw player blip (centerObject)
    25. //  bX=centerObject.transform.position.x * mapScale;
    26. //  bY=centerObject.transform.position.z * mapScale;   
    27. //  GUI.DrawTexture(Rect(mapCenter.x - mapWidth/2,mapCenter.y-mapHeight/2,mapWidth,mapHeight),pauseButton);
    28.    
    29.     // Draw blips for Enemies
    30. //  DrawBlipsForEnemies();
    31.  
    32.  
    33.  
    34. }
    35.  
    36.  
    37. function doPauseToggle()
    38. {
    39.  
    40. if(Time.timeScale > 0)
    41. {
    42.     pauseGame();
    43.  
    44. } else{
    45.     unPauseGame();
    46. }
    47.  
    48. }
    49.  
    50.  
    51. function pauseGame()
    52. {
    53.  
    54. Time.timeScale = 0;
    55.  
    56. }
    57.  
    58. function unPauseGame()
    59. {
    60.  
    61. Time.timeScale = 1;
    62.  
    63.  
    64. }
    65.  
    66.  
    67.  
    68.  
    69.  
    70.  
    71.  
    72.  
     
  2. Ericmatheus

    Ericmatheus

    Joined:
    Aug 11, 2014
    Posts:
    68
    I don't know if it gonna works but you need do in your button something like this
    var control:boolean;
    if touchphase==touch.began:
    if (control==true)
    control=false;
    PauseGame();
    else
    control=true;
    unPause()