Rin, or rather Rin's memory, will always be what motivates Obito and keeps him sane. When we see Obito's mind getting torn apart by the Juubi's, we can see that the only thing that keeps it intact are his feelings for Rin, he didn't want to lose her memory. So he pieced himself back together...