Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by zardosch · Jul 18, 2020 at 10:11 AM · script.disabled

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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().

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image zardosch · Jul 19, 2020 at 09:29 AM 0
Share

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?

avatar image Compii zardosch · Jul 19, 2020 at 10:26 AM 0
Share

Can you please post the code so I see where the variable gets changed now?

avatar image Bunny83 zardosch · Jul 19, 2020 at 10:45 AM 1
Share

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;
         }
     }
 }

avatar image Compii Bunny83 · Jul 19, 2020 at 11:05 AM 0
Share

I think he could check if any key is held down by using

 Input.anyKey
Show more comments
Show more comments
avatar image
0

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!

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Compii · Jul 19, 2020 at 12:48 PM 0
Share

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!

avatar image zardosch Compii · Jul 20, 2020 at 09:56 AM 0
Share

In this way it never can activate ToAuto

avatar image Compii zardosch · Jul 20, 2020 at 10:11 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

163 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges