Press button to perform Action 1. Same button for Action 2. Code skips to Action 2.

OK, sorry for the weird title but I’ve never been able to figure this one out as i’m still learning C# and Unity in general.

I have a script for my inputs. In this script i have:

public static bool Spawn()
{
	return Input.GetKeyDown ("e");
}

In another script i wish to spawn a preview of an enemy by pressing ‘E’. Then i want to press ‘E’ again and have it spawn the real thing.
This is the code:

public void SpawnMinion()
{
MPreview = Instantiate (MInionPreview, PlayerAiming.CroshairHit + (Vector3.up * 0.5f), Quaternion.identity);
PreviewSpawned = true;
}
        
if (PreviewSpawned == true) {
        
MPreview.transform.position = PlayerAiming.CroshairHit + (Vector3.up * 0.5f);
        
if (PlayerInput.Spawn()) {
Destroy (MPreview);
PreviewSpawned = false;
MActual = Instantiate (MinionActual, PlayerAiming.CroshairHit + (Vector3.up * 0.5f), Quaternion.identity);}

However, because i am already pressing ‘E’ to spawn the preview, the game just skips the preview and takes that press and spawns the real thing. What’s the best way of not having this happen. Maybe cancelling the Input for a split second?

Thank you

As per my understanding with your code i suggest, u can take the count of pressing button,
let us say count=0,

  1. if on pressing button, count is 0-> perform action 1 and increment the count.
  2. then on pressing again the same button, if count=1, perform action 2, and reset the count to ‘0’.

I would suggest using another bool to detect when the key has been released, in addition to the one you have to check that the preview is active.

Check the button release with something like this in your Update:

if(Input.GetKeyUp("e"))
{
    ActionKeyReleased = true;
}

Then setup your spawn condition as something like this:

if (PlayerInput.Spawn() && ActionKeyReleased)
{
    Destroy (MPreview);
    PreviewSpawned = false;
    MActual = Instantiate (MinionActual, PlayerAiming.CroshairHit + (Vector3.up * 0.5f), Quaternion.identity);

    ActionKeyReleased = false;
}