Sanctifying grace (the life of God in our soul) is completely lost by mortal sin. One must go to confession to restore it. The Holy Spirit will not dwell where He is not welcome (in a soul deadened by mortal sin). We cannot gain sanctifying grace by attending Mass or doing other good works while in the state of mortal sin. (It still grieves me to think about how I “wasted” years of going to Mass and unworthily receiving Communion when I was not in the state of grace. I thank God that, in His infinite mercy, He brought me back through the Sacrament of Reconciliation.)
There is another kind of grace, though — actual graces. These are like “interventions” God gives us when we need them: inspirations to do a good work, to pray, to repent, to go to confession. These can be received even when we are not in the state of sanctifying grace. This may be what is happening to you now (if it is yourself you are asking about; you might be writing on behalf of someone else, or out of curiosity). Responding to actual graces can lead us to repentance and to restoration of sanctifying grace.
I would suggest that a person in the state of mortal sin continue attending Mass and offer each Mass for the courage to go to confession. Then, after finally cooperating with the actual graces and responding to God’s call to go to confession (what a joyous day that could be!), they could go to Mass every day with a clean conscience, be free to receive Communion, and offer those Masses and Communions for any intentions, with the assurance that they are gaining grace for themselves and for the people they are praying for.
I went for a period of about twenty years without making a complete confession. When I finally drummed up the courage to make a good confession, it was the most wonderful experience I can ever remember having. Yes, it was difficult and a little embarrassing, but totally worth it! It changed my life, and I found joy in things where before I had only found discontent, emptiness, or anger. The devil wants us to stay away from confession by making us think it is a bigger step than we are able to take. In fact, when I finally took that step, I wondered why I had waited so long.