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.
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.
Did you put a debug.log into your if statement to see if it is firing multiple times on a single keypress?
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.
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.
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?
@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?
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.
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.
Your answer

Follow this Question
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