A word used to describe doing something good to unknowingly feel better about yourself?

I'm writing a story analysis and there's this character who is good and especially loyal to his friend. However, while he thought he was being nice for friendship, he eventually realises that he did it to feel better about himself.

What word describes this situation?


Self-serving behavior is distorted by the need to maintain and enhance self-esteem, or the tendency to perceive oneself in an overly favorable manner. It describes the aspect of being nice but with other motivations.

The realization that he was being nice and loyal in order to feel better about himself (rather than for the sake being a good friend) occurs when he becomes self-aware.