Scotland's finance secretary Derek Mackay quit just hours before he was due to present the budget on Thursday, saying he had "behaved foolishly" in a string of messages to a 16-year-old schoolboy.The 42-year-old apologised to the teenager and First Minister Nicola Sturgeon accepted his resignation.Mackay suddenly stepped down shortly before he was set to unveil the Scottish government's budget for the next financial year, after a newspaper exposed his messages.The Scottish Sun reported that he had befriended the schoolboy on Facebook and Instagram and sent him 270 social media messages over a six-month period.In one message he is alleged to have asked: "And our chats are between us?" and when the boy agreed, told him: "Cool, then to be honest I think you are really cute."The tabloid said Mackay offered to take him out to dinner and a rugby game."I take full responsibility for my actions. I have behaved foolishly and I am truly sorry," Mackay said in a statement."I apologise unreservedly to the individual involved and his family."I spoke last night with the first minister and tendered my resignation with immediate effect."Serving in government has been a huge privilege and I am sorry to have let colleagues and supporters down."Rising star Mackay had been the finance minister since 2016. He was seen as a rising star of Scottish politics.The father-of-two announced in 2013 that he had separated from his wife, adding that it was "important for me to be clear publicly that I am gay".The Scottish budget will now be presented by junior public finance minister Kate Forbes, 29.Within the United Kingdom, Scotland has its own devolved parliament, with its government deciding policy and spending on areas such as health and education.The left-wing, secessionist Scottish National Party runs a minority administration in Edinburgh, and is pushing for a second referendum on Scottish independence.In accepting his resignation, SNP leader Sturgeon said Mackay "recognises that his behaviour has failed to meet the standards required".The Scottish Sun published full threads of messages dating from August 2 last year until February 1.Mackay contacted the boy out of the blue and asked what age he was, saying "oh right" after being told he was 16.The teenager's mother, 37, learned of the exchanges last week."If I could speak to him, I would ask him 'Why? Why did you do this?'," she told the Scottish Sun."You can see he tries again and again -- like he is trying to get my son to change his mind about something."He is trying to see how much he can push it."Murdo Fraser, finance spokesperson for the main opposition Conservatives in the Scottish Parliament, said Mackay was popular across the chamber."This is, however, a huge blow for Nicola Sturgeon to lose such a key member of her government at such a crucial time - and one tipped as her successor.