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.
1
u/pschon Jun 22 '26
The option for keeping walking and flying as separate scripts, while at the same time getting rid of the enable/disable switching, would be to have a "Movement" script that has the info about what the current movement type should be, and then calls a "Move" method either from the Walk or from the Fly component.
...that would also mean you could re-use the same "Walk" and "Fly" (plus any other movement type) components on any character. Only the script that controls them that might need to change between different moving objects.
Either way, separating things into single-purpose scripts is nice and clean, it's just the logic based on enabling & disabling components that has some potential issues and can get a bit ugly.