- Home /
Disabling a script through code while that script is working
Hello,
I have a car GameObject which needs to switch between selfdriving--simply following the predefined pathway-- when user don't press any key for 3 sec; and manual driving when user press arrow key . For self-driving and manual I have created separate scripts which both of them attached to the car. But auto script doesn't stop working when it is in self-driving mode. here is the code:
void Awake()
{
car = GameObject.Find("Car");
manual = car.GetComponent<DriveManual>();
automatic = car.GetComponent<SelfDriving>();
}
void Update()
{
ToManual();
ToAuto();
}
public void ToManual()
{
if (Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.UpArrow))
{
automatic.enabled = !automatic.enabled;
manual.enabled = true;
automatic.enabled = false;
}
}
float leftTime = 3f;
public void ToAuto()
{
if (!Input.anyKeyDown)
{
leftTime -= Time.deltaTime;
if (leftTime < 0f)
{
automatic.enabled = true;
manual.enabled = false;
}
}
}
This switch should take place as a result of triggering another event. I'll be thankful for any hints and idea
Answer by Compii · Jul 18, 2020 at 10:40 AM
It looks like you should try to set your leftTime variable to 3 after you set manual.enabled to false in ToAuto(), because otherwise it will stay below 0 and therefore enable automatic every time you call ToAuto().
Thank you very much for the hints! I didn't notice it. but now I have another problem:
now it can switch To$$anonymous$$anual but it remains active for just the period of 'leftTime' -- although I keep pressing the arrow keys on the keyboard. I can't figure it out; it somehow overrides the condition in ToAuto.
Got any idea how to fix it?
Can you please post the code so I see where the variable gets changed now?
Input.anyKeyDown does not return the state of all keys but returns true only for one frame when a key has been pressed down. This is part of the text inputting part of Unity. AFAIK there's no direct way to test if "any" key is held down. What you can do is simply checking is any of your control keys is held down.
Something like this should work:
void Update()
{
if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.UpArrow))
{
manual.enabled = true;
automatic.enabled = false;
leftTime = 3f;
}
else
{
leftTime -= Time.deltaTime;
if (leftTime < 0f)
{
automatic.enabled = true;
manual.enabled = false;
}
}
}
I think he could check if any key is held down by using
Input.anyKey
Answer by zardosch · Jul 19, 2020 at 10:50 AM
public class SwitchController : MonoBehaviour
{
[SerializeField]
private GameObject car;
private DriveManual manual;
private SelfDriving automatic;
void Awake()
{
car = GameObject.Find("Car");
manual = car.GetComponent<DriveManual>();
automatic = car.GetComponent<SelfDriving>();
manual.enabled = false;
automatic.enabled = false;
}
// Start is called before the first frame update
void Start()
{
EventTrigger.current.onCheckPoinEnter += EnterTriggerBox;
EventTrigger.current.onCheckPointExit += ExitTriggerBox;
}
// Update is called once per frame
void Update()
{
ToManual();
ToAuto();
}
public void ToManual()
{
if (Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.UpArrow))
{
//if (leftTime == 3f)
//{
automatic.enabled = !automatic.enabled;
manual.enabled = true;
automatic.enabled = false;
//}
}
}
float leftTime =3f;
public void ToAuto()
{
if (!Input.anyKeyDown)
{
//float leftTime =3f;
leftTime -= Time.deltaTime;
if (leftTime < 0f)
{
automatic.enabled = true;
manual.enabled = false;
leftTime = 3f;
}
}
}
}
here is the code; Thanks in advance!
As @Bunny83 mentioned above Input.anyKeyDown just works for one frame, if you keep pressing a Button it wont return true anymore and therefore automatic will be enabled after 3 seconds. You can change it to Input.anyKey which should work if the key is still held down.
Just some more suggestions: If you dont press a Key for like 1 second and then again, leftTime will be 2 seconds the next time you dont press any key anymore but it should probably be 3 seconds again, so you just need to add an else statement to if(!Input.anyKey) which sets leftTime to 3:
if (!Input.anyKeyDown)
{
//float leftTime =3f;
leftTime -= Time.deltaTime;
if (leftTime < 0f)
{
automatic.enabled = true;
manual.enabled = false;
leftTime = 3f;
}
}else{
leftTime = 3f;
}
And one more suggestion: You can remove automatic.enabled = !automatic.enabled; in to$$anonymous$$anual() because you change automatic.enabled to false two lines below which makes the inverse useless. Hope it works now!
Well yes because Input.anykey doesnt work is i expected. So rather set leftTime to three in To$$anonymous$$anual(). Since anyKeyDown and anyKey dont work as you need it, i'd recommend setting an int which counts the pressed buttons and check if its 0:
int keysPressed = 0;
void Update()
{
if (Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.UpArrow))
{
keysPressed++;
}
if (Input.GetKeyUp(KeyCode.RightArrow) ||
Input.GetKeyUp(KeyCode.LeftArrow) ||
Input.GetKeyUp(KeyCode.UpArrow))
{
keysPressed--;
}
To$$anonymous$$anual();
ToAuto();
}
public void To$$anonymous$$anual()
{
if (Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.UpArrow))
{
manual.enabled = true;
automatic.enabled = false;
leftTime = 3;
}
}
float leftTime = 3f;
public void ToAuto()
{
if (keysPressed == 0)
{
leftTime -= Time.deltaTime;
if (leftTime < 0f)
{
automatic.enabled = true;
manual.enabled = false;
leftTime = 3f;
}
}
}
}
Your answer
Follow this Question
Related Questions
How do I enable my scripts on default?? 3 Answers
how to select UI button with script 1 Answer
fps movement limited horizontal and vertical both 0 Answers
Activate seperated dolly tracks 0 Answers
UI ToolKit Missing Scripts? 0 Answers