So, I have two ideas how to do that:

  • Use enum to define current state and check current state value before running certain method group in Update. Code:

    void Update()
        case 1:
        case 2:

    And so on. I don't think that this is a good idea because of possible performance drops.

  • Same concept about current state, but instead of checking it and running method groups, "head" script will check current state and activate corresponding script, disabling others. For me it looks like best way in terms of performance and usability.

And also, is it better to keep all input checks in one giant script/component and call functions you need from it? Essentially creating centralized Input Manager.

  • 3
    \$\begingroup\$ "possible performance drops" are not a real problem. Actual, measured performance drops are a problem. So: did you try writing and running this code? Did a single switch really turn out to be a substantial drain on your performance? If not, go do that before asking here. Both these options are valid - it's up to you what works better for the style of coding you and your team prefer for the needs of your current project. \$\endgroup\$
    – DMGregory
    Jul 27 '19 at 3:59

You are prematurely optimizing and wasting precious time and brain juice.

Deciding where an if/switch should go when it's called only once or so per frame is irrelevant, most of the time.

Until you have your game quite developed, you should not worry about such optimizations. Then use a profiler to see where the bottlenecks are and fix and optimize actual issues.

Meanwhile, write the code that is the clearest for you and your team. In 6 months from now, you'll be glad you wrote clean and clear code because it will make it much easier to maintain.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.