SANTA CLARA, Calif. (AP) — San Francisco's previously dormant offense woke up with five touchdowns, 421 yards and more than enough big plays to win a game.

But a worn-down defense and several missed opportunities for more points ended up dooming San Francisco, preventing what was nearly an epic comeback.

A missed extra point by Robbie Gould. A failed attempt at a 2-point conversion that would have tied the game. An offensive pass interference that took the 49ers from the fringe of game-winning field goal range to a desperate situation that ended with a fourth-down sack.

That all added up to a 41-39 loss to the Los Angeles Rams on Thursday night that left San Francisco still seeking its first win under new coach Kyle Shanahan.

"If you have mentally strong people, I believe we can get better from all this stuff and I believe we do have the right guys in that room," Shanahan said. "I don't really care about the record right now. I care about how guys are responding to it."

The game started with Brian Hoyer throwing an interception on the first play from scrimmage for San Francisco (0-3), leading to the first of Todd Gurley's three touchdowns and a 7-0 deficit just 12 seconds into the game.

But the offense picked up after that with Hoyer delivering San Francisco's long-awaited first touchdown of the season with a 9-yard run .

"To start out with the first play as a pick, basically a pick six, I think they took it to the 2. I knew that was going to be tough, so I just went up and talked to him, looked in his eyes, he looked fine," Shanahan said. "He said, 'I'm ready to go. I'll be all right.' He responded pretty well the next drive."

Hoyer also had success through the air, throwing for 332 yards and two touchdowns as he found a rhythm with Pierre Garcon (seven catches for 142 yards) and hit Marquise Goodwin on San Francisco's first successful deep ball of the game with a 50-yard gain.

Carlos Hyde also got into the mix by rushing for 84 hard-fought yards and two fourth-down touchdowns, but it wasn't enough to get San Francisco out of a late 15-point deficit after Jared Goff connected on a 13-yard pass to Sammie Watkins for his third TD of the game with 8:43 to play.

The teams traded punts before Hoyer found Garcon for a 59-yard pass that set up a 3-yard TD pass to Trent Taylor that made it 41-33.

Pharoh Cooper then fumbled the ensuing kickoff, setting up the 49ers for another score. Hyde delivered with a 1-yard run on fourth down with 2:13 to go but San Francisco was unable to tie it with a 2-point conversion when Troy Hill broke up a pass to Taylor .

"That was the same route that Trent scored on earlier," Hoyer said. "He sells an out and comes back in. He actually beat the guy out. So the guy, when he came back to run back in, the guy was kind of there waiting for him."

The Niners were only going for 2 because Gould missed an extra point earlier in the fourth quarter.

"I just rushed it, I missed it, I made a mistake," Gould said. "Obviously, I wish I didn't do that, or we'd probably be playing in overtime right now. But at the end of the day, they're counting on me to do my job and I didn't make the extra point I needed to make."

Gould did execute a perfect onside kick that San Francisco recovered at midfield. But Mark Barron made a good stop to prevent a potential long gain on a short pass to Hyde, and then Taylor was called for pushing off on the next play, negating an 11-yard completion to the Los Angeles 39.

An incomplete pass and a sack by Aaron Donald on fourth down ended the comeback attempt.

"Once we put it together, it'll be good," Hoyer said. "We have to play a complete game. When the defense plays well, we've got to score points. When we play well, we all got to play together and figure out a way to win. Whatever it takes."

___

More AP NFL: pro32.ap.org and twitter.com/AP_NFL