Need a script to hide the player

I am new to unity and scripts in general so I thought I would learn through trial. And I would greatly appreciate a detailed answer.
The game offer the player to hide behind “certain” objects and it’s 2D game. So I think I need OnCollisionEnter and buttonDown to destroy the player and the object hiding behind it and replace them with a certain spirit that shows that the player is hiding and of course disable the collider, and when the player takes his hand off the button the character returns to its original state.
Thank you.

You got the logic of it correct, but you don’t want to destroy your player, it would be easier to just hide it from view. There are multiple ways to do this, the one you should pick depends on your situation.

Using sorting layer

You could set the sorting layer to one that is behind the background so your background draws over your player effectively hiding the player from view. But all other player mechanics will still be active

Set inactive

You could set your players game object to inactive by calling gameObect.SetActive(false). This will stop drawing and updating your character, but it will also stop updating your components as well if I am thinking correctly.

Z position

If you don’t have your sorting layers set up you could also move your player around in the Z axis, which will draw it ahead or behind all objects that are ahead or behind the player but all other player mechanics will still be active