- Home /
Question by
JoshPriceGames · Jun 08, 2019 at 12:25 AM ·
script.enable and disable script
Disable FirstPersonController.cs when I click on a object, then re-enable it when I hit Q
Basically, I have a JS that shows GuiText when I click ("interact") with an object. When the player presses q, the text clears away.
I want FirstPersonController.cs to be disabled when the text is open (So, when I click on the object) then when I hit q, it clears the text and ALSO enables the script again at the same time.
Here is the script that changes/clears the text (In JavaScript)
var TextPart : UnityEngine.UI.Text;
var Message : String = "Write Here";
function OnMouseDown () {
TextPart.text = Message;
}
function Update()
{
if(Input.GetKeyDown(KeyCode.Q))
TextPart.text = "";
}
Comment
Best Answer
Answer by BradyIrv · Jun 08, 2019 at 09:48 PM
I'm not sure how much would change for JS, but in C#:
// Reference to the component, either set in inspector or through code
public FirstPersonController controller;
// Example setting through code, assuming GameObject with script is tagged Player
private void Start()
{
controller = GameObject.FindWithTag("Player").GetComponent<FirstPersonController>();
}
// Add code to disable and enable component to events
private void OnMouseDown()
{
controller.enabled = false;
// Text update
}
if(Input.GetKeyDown(KeyCode.Q))
{
controller.enabled = true;
// Text update
}
If I had to guess the JS variant to referencing the component
var controller : FirstPersonController;
Your answer
![](https://koobas.hobune.stream/wayback/20220612202820im_/https://answers.unity.com/themes/thub/images/avi.jpg)