Search Unity

Bool variable doesn't updating (C#)

Discussion in 'Scripting' started by DeanAseraf1, Oct 6, 2019.

  1. DeanAseraf1

    DeanAseraf1

    Joined:
    Aug 3, 2018
    Posts:
    16
    Hello:)
    i have a problem that i do not understand in one of the projects I'm currently working on.

    Iv'e created a prefub of an empty object attached to a script called "GamePause".
    In the GamePause script I wrote a public void functions called Pause and Resume.
    I wrote this script cause I want to be able to pause the game in any time with a number of different scripts and functions, because the game have a lot of different menus and times when the game needs to be pause and then resume again.
    I've created a new scene and drag the GamePause gameobject over there with all the required menus, BUT,

    for some reason the bool I declared(GameRunning) do not want to be updated to false when I call the pause function. even tho it works perfectly in the other scene.

    this is the GamePause script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityStandardAssets.Characters.FirstPerson;
    4. using UnityEngine;
    5.  
    6. public class GamePause : MonoBehaviour {
    7.     public CharacterController cc;
    8.     public bool GameRunning;
    9.     public FirstPersonController player;
    10.     public Cam Camcam;
    11.  
    12.    
    13.  
    14.     void Start () {
    15.         GameRunning = true;
    16.     }
    17.    
    18.     void Update () {
    19.  
    20.         if(GameRunning == false)
    21.         {
    22.             Cursor.visible = true;
    23.             Cursor.lockState = CursorLockMode.None;
    24.             Pause();
    25.            
    26.         }
    27.         else
    28.         {
    29.             Cursor.visible = false;
    30.             Cursor.lockState = CursorLockMode.Locked;
    31.             Resume();
    32.            
    33.         }
    34.        
    35.     }
    36.     public void Pause()
    37.     {
    38.        
    39.         GameRunning = false;
    40.         player.enabled = false;
    41.         cc.enabled = false;
    42.         Camcam.enabled = false;
    43.     }
    44.     public void Resume()
    45.     {
    46.         GameRunning = true;
    47.         player.enabled = true;
    48.         cc.enabled = true;
    49.         Camcam.enabled = true;
    50.     }
    51. }
    52.  
    I need to reference all these variable because I'm using the Unity first person standard asset prefub.
     
  2. Flynn_Prime

    Flynn_Prime

    Joined:
    Apr 26, 2017
    Posts:
    387
    You're only calling Pause() if Game Running is already equal to false, but it only gets set to false within the Pause() method.

    You need to set the variable to false by other means outside of that method.

    EDIT: You will also run into a similar issue with the Resume()
     
  3. DeanAseraf1

    DeanAseraf1

    Joined:
    Aug 3, 2018
    Posts:
    16
    I'm also calling pause in other scripts, by refrencing the GamePause class and than calling the function that i want to use,

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityStandardAssets.Characters.FirstPerson;
    4. using UnityEngine;
    5.  
    6. public class GamePause : MonoBehaviour {
    7.     public CharacterController cc;
    8.     public bool GameRunning;
    9.     public FirstPersonController player;
    10.     public Cam Camcam;
    11.  
    12.    
    13.  
    14.     // Use this for initialization
    15.     void Start () {
    16.         GameRunning = true;
    17.     }
    18.    
    19.     // Update is called once per frame
    20.     void Update () {
    21.  
    22.         if(GameRunning == false)
    23.         {
    24.             Cursor.visible = true;
    25.             Cursor.lockState = CursorLockMode.None;
    26.            
    27.         }
    28.         else
    29.         {
    30.             Cursor.visible = false;
    31.             Cursor.lockState = CursorLockMode.Locked;
    32.            
    33.         }
    34.        
    35.     }
    36.     public void Pause()
    37.     {
    38.        
    39.         GameRunning = false;
    40.         player.enabled = false;
    41.         cc.enabled = false;
    42.         Camcam.enabled = false;
    43.     }
    44.     public void Resume()
    45.     {
    46.         GameRunning = true;
    47.         player.enabled = true;
    48.         cc.enabled = true;
    49.         Camcam.enabled = true;
    50.     }
    51. }
    52.  
    when the script is like this, its works the same way.
     
  4. DeanAseraf1

    DeanAseraf1

    Joined:
    Aug 3, 2018
    Posts:
    16
    and also, this script works when i run it in the first scene so I don't see any reason why it shouldn't work when I'm using the prefub again in a new one.

    Example of calling the Pause function in another script(quit menu manager):

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EscMenu : MonoBehaviour {
    6.  
    7.     public GameObject ESCan;
    8.     public GamePause GP;
    9.  
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.        
    14.     }
    15.    
    16.     // Update is called once per frame
    17.     void Update () {
    18.         if (Input.GetKeyDown(KeyCode.Q))
    19.         {
    20.  
    21.             if (GP.GameRunning == true)
    22.             {
    23.                 GP.Pause();
    24.                 ESCan.SetActive(true);
    25.             }
    26.             else if (GP.GameRunning == false)
    27.             {
    28.                 GP.Resume();
    29.                 ESCan.SetActive(false);
    30.             }
    31.  
    32.         }
    33.  
    34.     }
    35. }
     
  5. DeanAseraf1

    DeanAseraf1

    Joined:
    Aug 3, 2018
    Posts:
    16
    Ok so, I don't know why but for some reason setting up the new scene again seems to solve the issue.
    thanks:)