This relates to what the catechism calls “formation of conscience.” We are not born with perfect knowledge of what’s good or evil. When we reach the “age of reason” (usually taken to be about age 7), we do not suddenly have it all sorted out. In fact, we never, in this life, have it all sorted out. We are continually educating and clarifying our conscience.
When a situation arises, like the one you described, we make a quick decision, or perhaps just act without even thinking. Later we recognize that we may have sinned. For us, that can be an opportunity to learn, and to grow in virtue. Resolve to do better. Make restitution if necessary. Seek forgiveness. Give thanks. Go on living.
Here is a relevant section from the Catechism of the Catholic Church:
The Formation of Conscience
1783 Conscience must be informed and moral judgment enlightened. A well-formed conscience is upright and truthful. It formulates its judgments according to reason, in conformity with the true good willed by the wisdom of the Creator. The education of conscience is indispensable for human beings who are subjected to negative influences and tempted by sin to prefer their own judgment and to reject authoritative teachings.
1784 The education of the conscience is a lifelong task. From the earliest years, it awakens the child to the knowledge and practice of the interior law recognized by conscience. Prudent education teaches virtue; it prevents or cures fear, selfishness and pride, resentment arising from guilt, and feelings of complacency, born of human weakness and faults. The education of the conscience guarantees freedom and engenders peace of heart.
1785 In the formation of conscience the Word of God is the light for our path; we must assimilate it in faith and prayer and put it into practice. We must also examine our conscience before the Lord’s Cross. We are assisted by the gifts of the Holy Spirit, aided by the witness or advice of others and guided by the authoritative teaching of the Church.