Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Brytek · Jun 02, 2016 at 11:26 PM · enable and disable script

Enabling/Disabling 2 scripts with one button

I have 2 playable characters that I would like to switch between. I created 2 separate playermovement sripts and attached to the characters. Then I created a switch controls sript and attached it to both of the characters:

 public BearMovement BM;
 public PlayerMovement PM ;

 void Start ()
 {
    BM = GetComponent<BearMovement>();
    PM = GetComponent<PlayerMovement>();
 }

 void Update()
 {
     if (Input.GetKeyDown(KeyCode.Space))
     {
         PM.enabled = !PM.enabled;
         BM.enabled = !BM.enabled;
     }
 }

The switch controls script seems to work, but it only disables/enables playermovement script from first line after "if". Why doesnt it enable/disable both of the sripts whe written like this? Is there a way to fix it? When I divide the scripts into two separate "if"s with GetKeyUp it works. But ideally I would like it to happen simultaniusly when pressing space down.

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
0
Best Answer

Answer by Brytek · Jun 02, 2016 at 11:49 PM

Thank for the explanation. I changed GetKeyDown for GetKeyUp as it suits my purpose better. It still doesnt change the state of BM script. Pressing space only disables/enables the PM script.

Comment
Add comment · Show 7 · 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 Static-Dynamo · Jun 02, 2016 at 11:52 PM 0
Share

Did you put a debug.log into your if statement to see if it is firing multiple times on a single keypress?

avatar image Brytek Static-Dynamo · Jun 03, 2016 at 12:03 AM 0
Share

Haven`t used debug.log before I am not sure how to do this. But according to Your explanation above, having Get$$anonymous$$eyUp assures that it is firing only once.

avatar image Static-Dynamo Brytek · Jun 03, 2016 at 12:06 AM 0
Share

Ah, ok well I'm glad it helped.

About Debug.Log:

Example: Debug.Log("Your message here");

Will print a whatever is in the quotation marks, in this case:

Your $$anonymous$$essage here

into the unity console when the code hits it. It is a useful way to check what something is doing, or to see how many times something is called etc.

http://docs.unity3d.com/ScriptReference/Debug.Log.html

Show more comments
avatar image Static-Dynamo · Jun 03, 2016 at 12:47 AM 0
Share

That is very very strange. The script should be executing everything within the brackets underneath the if statement when the condition is met. The only way I can think of where it will execute the first line of code and the first line only is if that line of code is somehow disabling the script as well and so it doesn't progress to the next line because it is disabled.

Very odd. Especially since you say it works if you just break it up into two different if statements. Could there be something else going on in another script somewhere?

avatar image Static-Dynamo · Jun 03, 2016 at 01:05 AM 1
Share

@Brytek - You have this script on both players? Do both players have Bear$$anonymous$$ovement and Player$$anonymous$$ovement components?

I think this might be what is happening

Player- Has Player$$anonymous$$ovement component and controller switch component.

GetComponent() results in nothing, because the player doesn't have that component attached to it. Therefore the script on the player cannot disable/enable it.

Bear- Has Bear$$anonymous$$ovement component and controller switch component. GetComponent() results in nothing, because the bear doesn't have that component attached to it. Same as above.

GetComponent<> will only return components attached to the same thing the script is attached to. So unless you have both Player$$anonymous$$ovement and Bear$$anonymous$$ovement on the same object as the script, it will not work correctly.

https://docs.unity3d.com/ScriptReference/GameObject.GetComponent.html

Check your console tab while the game is playing and you try to get your script switch to go, does it give you any errors or messages?

avatar image Brytek Static-Dynamo · Jun 03, 2016 at 10:58 PM 0
Share

Thank you for the insight, that is correct. I created 2 separate switch control scripts for both characters enabling/disabling playermovementscript separately. It works great.

avatar image
0

Answer by Static-Dynamo · Jun 02, 2016 at 11:40 PM

Quick explanation of GetKeyDown vs GetKeyUp

Update fires as fast as your computer can calculate the code. GetKeyDown fires constantly as long as it can detect that the button is pressed. This means even if you think you have just tapped the key, the time the key is down might be long enough that your processor can go through your code multiple times. It might be disabling and enabling them multiple times in a single keypress making it seem like it's not doing anything, when it actually is. Set a debug.log message in your if statement and see how many times it prints a message on a single keypress.

GetKeyUp fires once and only once, when the key returns to its default position. This means your code will only execute once.

Comment
Add comment · 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

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

58 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

Related Questions

Cant Enable And Disable Scripts 1 Answer

Reference a script on a gameobject that is disabled in scene 0 Answers

Enable/Disable other gameobject's scripts Unity 5 0 Answers

Timeline animation frozen after being disabled 1 Answer

How to enable only one script (shared by multiple gameobjects) at a time/click ? 1 Answer


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