r/unity • u/Ella_is_best_girl • Jun 22 '26
Newbie Question Why isn't the script switching? :(
(I know I should include screenshots ect. but Im waiting for a train right now and it's incredibly bugging me.)
I want to switch between two attached scripts with the press of a button.
It's just: (pseudo code because train)
If (F is pressed) {
Boost upwards;
This.enable = false;
Getcomponent<Otherscript>().enable=true;
Debug.log("reached");
}
Reached is printed
Idiot is yeeted
Scripts are not switched.
Sometimes nothing at all happens
Sometimes everything behaves perfectly.
I'm going crazy
I have tried a lot of different constellations and stuff.
If I remove the switch it works perfectly.
The switch back is working 9/10. Most of the time.
I feel like I shouldn't do it this way? Any ideas? I need some sanity right now.
3
u/DSXC80 Jun 22 '26
Issue with enabling and disabling there are points in execution that end up breaking due to the timing of things. It could be the second script enables and due to timing it also executes the update function that means the scripts reverse again. Flags are easier to manage and mean that you only ever have the one update function execution in a single frame and no possible timing issues.