Don't know how to use SetActive() properly - c#

So, my script that I've written isn't working fully. I have A pause button and when I press it, it triggers my bool and shows that its working properly but when I'm "Paused" my UI doesn't pop up and my game doesn't stop in the back ground. I hope you can understand clearly. I am a beginner!
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class PauseManager : MonoBehaviour {
public GameObject pauseMenu;
public bool paused = false;
public void start()
{
pauseMenu.SetActive(false);
}
public void update()
{
if(Input.GetButtonDown("escape"))
{
paused = !paused;
}
if (paused)
{
pauseMenu.SetActive(true);
Time.timeScale = 0;
}
if (!paused)
{
pauseMenu.SetActive(false);
Time.timeScale = 1;
}
}
public void Resume()
{
paused = false;
}
public void pauseButton()
{
paused = true;
}
}

Advice you change your code to this:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class PauseManager : MonoBehaviour {
public GameObject pauseMenu;
bool paused = false;
void Start()
{
pauseMenu.SetActive(false);
}
void Update()
{
if(Input.GetButtonDown("escape"))
{
paused = !paused;
}
if (paused)
{
PauseGame();
}
if (!paused)
{
ResumeGame();
}
}
void PauseGame(){
pauseMenu.SetActive(true);
Time.timeScale = 0f;
}
void ResumeGame(){
pauseMenu.SetActive(false);
Time.timeScale = 1f;
}
public void Resume()
{
ResumeGame();
}
public void pauseButton()
{
PauseGame();
}
}
I think the problem is on that when you pause button pressed, timescale be 0, and update does not work.

Related

Why wont my Pause screen come up when i click esc (in Unity)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PauseMenu : MonoBehaviour
{
public static bool gameIsPaused = false;
public GameObject PauseMenuUI;
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
if (gameIsPaused)
{
Resume();
} else
{
Pause();
}
}
Debug.Log("KeyInput");
}
public void Resume ()
{
PauseMenuUI.SetActive(false);
Time.timeScale = 1f;
gameIsPaused = false;
}
void Pause ()
{
PauseMenuUI.SetActive(true);
Time.timeScale = 0f;
gameIsPaused = true;
}
public void LoadMenu()
{
Debug.Log("Loading Menu...");
Time.timeScale = 1f;
SceneManager.LoadScene ("MainMenu");
}
public void QuitGame()
{
Debug.Log ("Quitting Game");
Application.Quit();
}
}
My code wont open the actual pause screen is there any reason why
i have tried editing it in different ways and i also tried some other code online but it never seems to work for me i have also went onto the unity forms and that wasn't really helpful

'public' is not valid for this item

Assets\GameControl2D.cs(25,10): error CS0106: The modifier 'public' is not valid for this item
This error is happening why? Please anyone let me help it to figure out.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameControl2D : MonoBehaviour
{
public static GameControl2D instance;
public GameObject gameOverText;
public bool gameOver = false;
void Awake ()
{
if (instance == null)
{
instance = this;
}
else if (instance != this)
{
Destroy (gameObject);
}
}
void Update ()
{
public void BirdDied()
{
GameOverText.SetActive (true);
gameOver = true;
}
}
}
You have a nested/local method BirdDied, that can't have access modifiers. It's accessible only from the method body of Update anyway. So this compiles:
void Update ()
{
void BirdDied()
{
GameOverText.SetActive (true);
gameOver = true;
}
}
But since you don't use it in your code, i doubt that it's what you want.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bird2D : MonoBehaviour
{
public float upForce = 200f;
private bool isDead = false;
private Rigidbody2D rb2d;
private Animator anim;
// Start is called before the first frame update
void Start()
{
rb2d = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
if (isDead == false)
{
if (Input.GetMouseButtonDown(0))
{
rb2d.velocity = Vector2.zero;
rb2d.AddForce(new Vector2(0, upForce));
anim.SetTrigger("Flap");
}
}
}
void OnCollisionEnter2D()
{
isDead = true;
anim.SetTrigger("Die");
GameControl2D.Instance.BirdDied();
}
}
This is the Bird2D.cs file i already remove public from birdDied method but the error is same

i cant make this pause menu work in unity

im trying to follow this video
and i think i did everything right but its not doing anything then i click the escape button
using System.Collections.Generic;
using UnityEngine;
public class PauseMenu : MonoBehaviour
{
public static bool GameIsPaused = false;
public GameObject pauseMenuUI;
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
if (GameIsPaused)
{
Resume();
}else
{
Pause();
}
}
}
public void Resume()
{
pauseMenuUI.SetActive(false);
Time.timeScale = 1f;
GameIsPaused = false;
}
void Pause()
{
pauseMenuUI.SetActive(true);
Time.timeScale = 0f;
GameIsPaused = true;
}
public void LoadMenu()
{
Debug.Log("loading menu");
}
public void QuitGame()
{
Debug.Log("quiting game");
}
}
this is how the panel looks like
it was before in Canvas but i moved it because i was trying to make it work but it didnt help
someone knows maybe how to fix it?

How can I fade out the ui text when enable it false?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SaveGameMessage : MonoBehaviour
{
public Text text;
public float speed;
public bool startFading = false;
public bool enableText = false;
private Color textColor;
private void Start()
{
text = GetComponent<Text>();
textColor = text.color;
}
private void Update()
{
if (startFading == true)
{
if (enableText == true)
{
text.enabled = true;
}
textColor.a = (Mathf.Sin(Time.time * speed) + 1.0f) / 2.0f;
text.color = textColor;
}
else
{
text.enabled = false;
}
}
}
And using it :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cinemachine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class PlayingInGameScenesController : MonoBehaviour
{
public CinemachineFreeLook[] freeLookcameras;
public LockController lockController;
public GameObject uiSceneText;
public GameObject player;
public float transitionSpeed = 5f;
public float thresHold = 0.1f;
public bool talkingProcess = true;
public SaveGameMessage saveGameMessage;
private bool newGame = true;
private void Start()
{
}
public void PlayingSceneInGame()
{
PlayingSceneStatesControls(true);
StartCoroutine(ScenePlayingTime());
}
private void Update()
{
if (SceneManager.GetActiveScene().name != "Main Menu" && newGame == true)
{
PlayingSceneInGame();
newGame = false;
}
}
private void LateUpdate()
{
}
private void PlayingSceneStatesControls(bool LockState)
{
lockController.LockControl(LockState);
if (LockState == true)
{
uiSceneText.SetActive(true);
}
else
{
talkingProcess = false;
uiSceneText.SetActive(false);
}
}
IEnumerator ScenePlayingTime()
{
yield return new WaitForSeconds(10);
PlayingSceneStatesControls(false);
freeLookcameras[0].enabled = false;
freeLookcameras[1].enabled = true;
var brain = Camera.main.GetComponent<CinemachineBrain>().m_DefaultBlend.m_Time;
saveGameMessage.enableText = true;
saveGameMessage.startFading = true;
player.GetComponent<Player>().SavePlayer();
StartCoroutine(FakeSave());
}
IEnumerator FakeSave()
{
yield return new WaitForSeconds(7);
saveGameMessage.startFading = false;
}
}
At the bottom I'm making fake for 7 seconds the problem is when the fake finish the ui text enable false at once. In the SaveGameMessage script at the bottom I'm doing :
text.enabled = false;
but that is ugly it's just stop the fade in out effect at once. I want somehow to enable it false but before that to finish the current fading so the text enabled false will be smooth fading out and ot just suddenly stopping the fading effect.
Before you disable it run an animation to fade it out. this animation should first be made or maybe you can find one online.
then just run the animation before setting it to false.

PauseMenu - FPS Camera still moves when in PauseMenu

Whenever I pause everything stops, that's how I want it to be, but the only thing that does not stop is the camera. I move my cursor the camera still follows. I want it to freeze. I am using the FPS Controller Asset in the asset store
using System.Collections;
using System.Collections.Generic;
using UnityStandardAssets.Characters.FirstPerson;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PauseMenu : MonoBehaviour
{
public static bool GameIsPaused = false;
static bool IsPaused = false;
public GameObject pauseMenuUI;
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
if (GameIsPaused)
{
Resume();
} else
{
Pause();
}
}
}
public void Resume()
{
pauseMenuUI.SetActive(false);
Time.timeScale = 1f;
AudioListener.pause = false;
GameIsPaused = false;
}
void Pause()
{
pauseMenuUI.SetActive(true);
Time.timeScale = 0f;
AudioListener.pause = true;
GameIsPaused = true;
}
public void LoadMenu()
{
Time.timeScale = 1f;
SceneManager.LoadScene("Menu");
}
public void QuitGame()
{
Debug.Log("Exiting Game");
Application.Quit();
}
}
Get the reference to the Character Controller Script and then disable it:
mycharfpscon.enabled = false;
And then enabble it back:
mycharfpscon.enabled = true;

Categories

Resources