An effect problem

I have a problem. The player does magic when you hold down the space key. I want the player to detect enemies at a certain distance and play an effect on them while the button is pressed.

For this, I tried the raycast and the instantiate method, but it plays in every frame because it is called inside the update function. But I just want only one effect to play on enemies while the button is pressed. I don’t know if I could explain what I mean. But how should I go about it?

I’m new to unity. I have to do this project for my final exam. Thanks in advice.

Here is the picture; cloning effects constantly.

private void Update()
if (Input.GetKey(KeyCode.Space))
//Place your code that you only want to active when the space press
The code above will make it so that the code inside of the if statement will only be active when the space bar is pressed in. So in your case I would place the raycast code inside of the IF statement so that it’s only active when the space bar is pressed down.

Hope this helps