LONDON — It was supposed to be one of the most important speeches of British Prime Minister Theresa May’s political career, one that would quell speculation over her leadership position and unite the Conservative Party behind her.

Instead, May’s keynote address at the Conservative Party Conference on Wednesday afternoon was like a very bad anxiety dream, marred by a prankster's interruption, a coughing fit and letters falling off the backdrop behind her.

To be sure, May made several policy announcements during the 65-minute address, including a pledge to put a price cap on energy bills. She said that Britain’s energy market wasn’t working and that it was time to stop “rip-off energy prices once and for all.” The announcement caused energy companies’ share prices to tumble.

She also said that she wanted to help young people get on the housing ladder and pledged an extra 2 billion pounds, or $2.65 billion, toward affordable housing.

The cost of housing in Britain has skyrocketed in recent years, especially in London, where the population has increased sharply but the housing stock has not.

May said she would take “personal charge” of getting the government “back into the business of building houses.”

The speech was also, at times, strikingly personal — May talked about her sorrow at not being able to have children and about her grandmother, who had been a domestic servant.

She also apologized for the general election earlier this year, in which the Conservative Party lost its parliamentary majority.

“We did not get the victory we wanted because our national campaign fell short,” she told the party faithful. “I hold my hands up for that. I take responsibility. I led the campaign. And I am sorry.”

May said the election campaign had been “too presidential” and “too scripted.”

Her performance Wednesday was anything but scripted.

Things started to go off the rails when a prankster approached May as she was speaking from the podium and handed her a P45, the British equivalent of a pink slip.The prankster joked that the dismissal notice was from Boris Johnson, Britain’s ambitious foreign secretary, who came under fire earlier in the day for saying that a coastal Libyan city, Sirte, could become a tourist destination once authorities “clear the dead bodies away.”

May is not a natural public speaker and isn’t known for her improvisation skills — indeed, she earned the nickname “Maybot” for her sometimes wooden, robotic style.

She seemed to be paralyzed at first when handed the P45 slip, but the crowd soon started shouting “Out!” “Out!” and the prankster, comedian Simon Brodkin, was escorted out of the building.

May then landed a jibe. “I was about to talk about someone I’d like to give a P45 to, and that’s Jeremy Corbyn!” she said, referring to the leader of the opposition Labour Party.

Then May started to cough, the bouts interrupting her speech numerous times. She sipped water. Her finance minister, Philip Hammond, passed her a lozenge. The audience gave her ovations to help buy her time.

As if things couldn’t get any worse, the letters on the backdrop behind her started to fall. In the video below, you can see the letter “F” fall off the slogan “Building a Country That Works for Everyone.” More letters would follow.

It was painful to watch at times, but it also won her sympathy in some quarters, including from Scottish First Minister Nicola Sturgeon.

And just as she did after the botched general election — in which she was widely perceived to have squandered an extremely strong position — May soldiered on.

“The test of a leader is how you respond when tough times come upon you,” she said at the conclusion of her speech, prompting applause.

It’s unclear whether her performance Wednesday will help or hinder her. But this much is true: Despite the many unexpected obstacles thrown her way during the speech, she pressed on with dogged determination and stoicism.

And then later, on Twitter, with humor.