To really be as clear as possible, let's go through the full sequence of steps outlined in the game rules:
- Combat begins.
- Players get priority.
- Player A declares which creatures are attacking.
- The unicorn's ability goes on the stack.
- Players get priority.
- Player B plays Shock, which goes on the stack on top of the unicorn's ability.
- All players pass priority, and Shock resolves.
- The unicorn dies.
- Players get priority.
- The unicorn's ability resolves. The other attacking creature is now 3/3.
- Players get priority.
- Player B declares blocks.
- Players get priority.
- Combat damage is dealt.
- Player B's 2/3 blocker dies, while player A's 3/3 attacker survives with 2 damage.
- Players get priority.
- Combat ends.
You may have noticed that players "get priority" a lot. Priority is the official game term for having a generic opportunity to "do something". Every time players get priority, they each declare, one at a time, in turn order starting with the player whose turn it is, whether they are doing anything, and what they are doing. When all players pass, the top thing on the stack resolves, and then players get priority again. When all players pass and the stack is empty, then the game moves on to the next part of the turn. Every time players get priority, any abilities that have triggered go on the stack, and state based actions such as creatures dying from damage happen, before the first player actually gets priority.
In practice, the strict formal process of priority is rarely followed. Instead, it's usually assumed that players pass most priorities when it's not their turn, until a player interrupts to state they're doing something.
Back to this specific situation, Player B can cast Shock at any of the times when players get priority.
- At step 2, you could kill the unicorn and prevent it from doing anything. Player A would not yet have declared attacks, however, and could therefore choose to not have his 2/2 creature suicidally attack into your 2/3 blocker.
- At step 5, you can kill the unicorn, but it's too late to stop its ability from happening. The unicorn dies, and Player A's creature is committed to the attack, but it will get the +1/+1 if you don't do something else to stop it.
- If you instead passed step 5, steps 6 through 9 wouldn't happen. At step 11, the next available priority, it is too late to kill the unicorn with Shock.