Yorick, in the scene you ask about (Act 5.1) in Shakespeare's Hamlet, was more than someone who was once nice to Hamlet, he was the court jester. He bore Hamlet on "his back a thousand times" and was kissed by Hamlet "I know not how oft."
A court jester was a clown who provided entertainment for the king and the royal household, but he was also often a friend and confidant. The role was one that allowed him to say whatever he wanted to or thought necessary to the king, without fear of reprisal. Perhaps, the jester was the only person in the kingdom who could do so.
In Hamlet, Yorick apparently spent much time with Hamlet when Hamlet was young.
Importantly, notice that the past has meaning for Hamlet here. He has changed since his opening soliloquy in which he compared the world to an unweeded garden (Act 1.2.135) and showed strong evidence that he was suffering from melancholy, or depression. All is not useless, now. The memory of Yorick has meaning for Hamlet. And Hamlet misses Yorick:
Where be your gibes now, your gambols, your songs, your flashes of merriment that were wont to set the table on a roar? Not one now to mock your own grinning? (Act 5.1.165-170)
Of course, in addition to Hamlet revealing that he misses Yorick, here, his line of thought also contributes to his past contemplations of existence, and forward to the contemplations to come:
Dost thou think Alexander looked o' this fashion i' th' earth?...And smelt so? Pah! (Act 5.1.175-78)
This is what human life comes to, for a jester or a great conqueror. But although Hamlet has not stopped thinking and contemplating, he is not made inactive by his thinking. In a minute or two, he will leap into action and leap into the grave with Ophelia, and declare:
...This is I,
Hamlet the Dane. (Act 5.1.233-34)
Hamlet has changed in more ways than one. From now on, as he says in Act 4.5.66:
My thoughts be bloody, or be nothing worth!