Sounds like a loose wire on one of the connections, possibly either ground make sure the ground is on the bare metal of the chassis to get a strong connection, then check your remote wire this is usually a thin blue wire this is your 12v signal wire when headunit turns on it sends a signal to the amp to power up.
I suspect a loose wire behind headunit, pull it out and have a nose make sure all connections are good.
Lastly, worth a check is pop the bonnet and check the live 12v feed (red) make sure this has good connection with the positive terminal on the battery. I had the same problem similar to yours on a old sub would work then cut out it turned out the 12v red cable under bonnet was loose!
But check all connections behind headunit also ground then your 12v feed red cable under bonnet.
Benny