Every time I have ever gotten an invalid opcode error the byte code of the compile contracts got corrupted or it was an error in ganache. If it works on a testnet, I would say your error is probably ganache.
Things to try:
- Delete your build directory and recompile (as you are using remix, change something and then recompile). This is to ensure your byte code did not get corrupted (can happen with lots of recompiles).
- Restart ganache. Good old fashion turning it off and on again. Sometimes fixes it.
- Update ganache and try it (if restarting it didn't work).
If the issue persists but your code works on a testnet, then assume it is ganache and there is nothing much you can do.
Hope this helps :)
NOTE: My problem was solved by disabling 'Error on transaction'. If using ganache GUI go to Settings >> Server >> disable Error on transaction.
Hope that to be helpful 😉