- Home /
Can't disable/enable GameObject Component (C#)
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!!
Answer by Landern · Dec 30, 2014 at 05:49 AM
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;
}
}
}
To take your "characterGO" object as a reference publicly manually or using GameObject.Find(). Then you have to do enable/disable that GO.
I got it to work but when I go to the pause menu while I'm walking it just keeps on walking.
Your answer
Follow this Question
Related Questions
Unable to enable script 0 Answers
How to disable/enable SSAOEffect or other specific Components? 1 Answer
enable/disable specific components 3 Answers
Disabling/Enabling components in children of children? 1 Answer
disable/enable script js 2 Answers