Opening night is sometimes impossible to script.

The Gophers had nine months to prepare for Thursday night’s game against Oregon State, and the coaches probably planned for hundreds of scenarios.

But who could predict Minnesota would have three players ejected for targeting? Who could have known Gophers true freshman defensive end Tai’yon Devers would force two fumbles, leading to first-half touchdowns?

And it was certainly unexpected for the Gophers to trail Oregon State, a 13-point underdog, by six points late in the third quarter. That’s when quarterback Mitch Leidner came to life, making big plays to set up the go-ahead score, then rushing for the clinching TD.

It wasn’t pretty, but Minnesota survived with a 30-23 victory before an announced crowd of 44,582 at TCF Bank Stadium.

“We shot ourselves in the foot. Defensively, they had us scrambling a little bit,” Gophers coach Tracy Claeys said. “The best thing about first games is they’re over.”

Leidner’s two rushing touchdowns gave him 25 for his career, breaking Rickey Foggie’s school record for quarterbacks.

Rodney Smith also rushed for two scores and carried 25 times for 125 yards.

The Gophers held on, despite losing those three players — Devers and linebackers Cody Poock and Jonathan Celestin — to targeting calls.

The targeting penalty was designed to prevent defenses from making head-to-head contact with ball carriers. Each called Thursday looked warranted, based on the replays.

“I don’t know if we had one [targeting call] in four years when I was defensive coordinator,” Claeys said, smiling. “So maybe Coach [Jay] Sawvel had them a little fired up.”

Gophers players agreed they were over-amped for opening night. It affected them on defense and offense, taking seven pre-snap penalties and making two errant snaps.

The Gophers also lost two key players to injuries — linebacker Nick Rallis (left arm) and wide receiver Rashad Still (shoulder).

Losing Poock, Celestin and Rallis quickly tested the Gophers’ depth at linebacker.

“It was kind of crazy,” safety Damarius Travis said. “Kind of nerve-racking just seeing all our starting guys go out like that. But I guess it’s part of the game. They got the rules changed, so we’ve gotta just follow them.”

Poll: Grade the Gophers performance against Oregon State.

Minnesota was in a serious rut when Oregon State took its 23-17 lead with 2:30 remaining in the third quarter.

Leidner took command on that next drive, though, scrambling for two huge first downs with the Gophers facing third-and-long. Smith capped that drive with an impressive 2-yard touchdown run, bouncing away from a tackle in the backfield and strolling into the end zone.

After Oregon State turned the ball over on downs with 8:57 left in the game, Leidner brought the Gophers back down the field and rushed 1 yard for the clinching score, barreling over defenders on his way to the end zone.

Leidner added this to his collection of comeback victories, along with Purdue (2014), Colorado State (2015) and Ohio (2015).

“I like to have the ball in my hand at those big moments to make those plays,” Leidner said.

Leading 30-23, an extra point would have given the Gophers an eight-point lead, but Claeys made the aggressive decision of going for the two-point conversion. Leidner tried hitting true freshman Tyler Johnson, but the pass fell incomplete.

Claeys later explained he would rather go for 3 yards right there to avoid letting Oregon State go down the field and tie the score. The Beavers still had a chance, but their last drive stalled at their own 33-yard line.

“It’s good to come out and get a win,” Leidner said. “Those aren’t easy to come by.”

The Gophers were able to overcome some ugly moments through three quarters.

They led 17-14 at halftime, despite being outgained to that point 210-122. The third quarter was worse.

Another errant snap bounced into the end zone for a safety. Oregon took the free kick and drove for the go-ahead touchdown, as a well-designed screen pass led to an easy 14-yard touchdown for running back Tim Cook. Just like that, the Beavers led 23-17.

But then the Gophers turned things on, playing their first game under new offensive coordinator Jay Johnson.

“It was a good start for the offense,” Claeys said. “We know we’ve got to be better, though.”