- Home /
Locking player script when menu is toggled?
Here I have a basic menu script which toggles my menu via "tab". I want to disable the player controller script I have (Named PlayerController) so that I dont continue to move around and play while the menu is up. I tried to do this via accessing the script PlayerController and setting it as "lockkeys", then using lockkeys to disable and enable it under the "tab" press functionality. It doesn't seem to work. Is there a better way to do this, or am I doing something wrong?
public class openmenu : MonoBehaviour {
public PlayerController lockkeys;
public Canvas IngameOption;
private bool menuEnabled = false; // call this whatever you wan
// Use this for initialization
void Start ()
{
IngameOption = IngameOption.GetComponent<Canvas>();
menuEnabled = false;
IngameOption.enabled = menuEnabled;
lockkeys = GetComponent<PlayerController> ();
}
// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown("tab"))
{
menuEnabled = !menuEnabled;
IngameOption.enabled = menuEnabled;
lockkeys.enabled = !lockkeys.enabled;
}
}
}
Answer by Komayo · Aug 10, 2017 at 11:22 PM
Just a idea, have you tryed using multithreading? Locking part of a script, until a value is returned. That way, the locked code cant be accessed by any other simultanious object running. You can adapt it to your situation as you seen fit better.
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/lock-statement
Hope it helps somehow. Iam not at home to give you a few examples of its awesome uses.
Your answer
![](https://koobas.hobune.stream/wayback/20220612132441im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
dynamically creating MenuItem via an xml File 2 Answers
Paused Menu backdrop 1 Answer
Load/Save script 0 Answers