SuperHB
December 30, 2014, 5:46am
1
The title says it all. I’ve tried searching up this problem and tried a bunch of the solutions, but they don’t work.
Here is my code:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class NetworkManager : MonoBehaviour
{
public GameObject pausePanel;
public GameObject characterGO;
private bool isPaused;
void Update()
{
if(Input.GetKeyDown(KeyCode.Escape) && isPaused == false)
{
isPaused = true;
pausePanel.SetActive (true);
Screen.showCursor = true;
Screen.lockCursor = false;
((MonoBehaviour)characterGO.GetComponent("FPSInputController")).enabled = false;
((MonoBehaviour)characterGO.GetComponent("MouseLook")).enabled = false;
characterGO.GetComponentInChildren<MouseLook>().enabled = false;
}
if(Input.GetKeyDown(KeyCode.Escape) && isPaused == true)
{
isPaused = false;
pausePanel.SetActive (false);
Screen.showCursor = false;
Screen.lockCursor = true;
((MonoBehaviour)characterGO.GetComponent("FPSInputController")).enabled = true;
((MonoBehaviour)characterGO.GetComponent("MouseLook")).enabled = true;
characterGO.GetComponentInChildren<MouseLook>().enabled = true;
}
}
}
I’m getting no errors but it doesn’t do anything… help!!
Landern
December 30, 2014, 5:49am
2
In a single pass in the Update method, if the first if statement is evaluated to be true(i.e. the Escape button is pressed and isPaused is false(which it would be by default due to your declaration above it)) then the second would also execute in the same pass(escape is pressed and isPaused is true since you just set it to the true right above). You need to probably use an if/else if statement block to constrain out that second evaluation during the first pass.
example:
public class NetworkManager : MonoBehaviour
{
public GameObject pausePanel;
public GameObject characterGO;
private bool isPaused;
void Update()
{
if(Input.GetKeyDown(KeyCode.Escape) && isPaused == false)
{
isPaused = true;
pausePanel.SetActive (true);
Screen.showCursor = true;
Screen.lockCursor = false;
((MonoBehaviour)characterGO.GetComponent("FPSInputController")).enabled = false;
((MonoBehaviour)characterGO.GetComponent("MouseLook")).enabled = false;
characterGO.GetComponentInChildren<MouseLook>().enabled = false;
}
else if(Input.GetKeyDown(KeyCode.Escape) && isPaused == true)
{
isPaused = false;
pausePanel.SetActive (false);
Screen.showCursor = false;
Screen.lockCursor = true;
((MonoBehaviour)characterGO.GetComponent("FPSInputController")).enabled = true;
((MonoBehaviour)characterGO.GetComponent("MouseLook")).enabled = true;
characterGO.GetComponentInChildren<MouseLook>().enabled = true;
}
}
}