Having code fire once in update function?

Hi, I’m a Unity noob (still learning) and I’m trying to build a basic space invaders clone to get my head around how everything works. Here is my enemy movement script, it’s working for left and right movement, but when I fire the jump variable, they either fly off the screen (because it keeps firing the jump code) or doesn’t jump at all (if I turn the variable off after it fires, the entire variable never even fires once).

I’ve tried Invoke instead, and placed the jump code in a function, but then I couldn’t work out how to have it affect all of the enemies (as it would just work on the one that triggered the jump scenario). I was playing with GameObject.FindGameObjectsWithTag (since all my enemies are tagged “Enemy”) but couldn’t work out how to then take that information and use it to move all the enemies down (sigh).

I’ve searched everywhere, so I’m hoping somebody can answer this simple question for me?

   function Update () {

 var amtToMove = enemySpeed * Time.deltaTime; 
 
 if (startMove==true)
 {
 transform.Translate(Vector3.forward*amtToMove);
 }
 
 if (transform.position.z >= 63)
 {
 startMove = false;
 JumpOn=true; 
 hitRIGHT = true;
 }
 
 if(transform.position.z <= -61)
 {
 hitRIGHT = false;
 JumpOn=true; 
 hitLEFT = true; 
 }
 
 if(hitRIGHT==true)
 {
 hitLEFT=false;
 transform.Translate(Vector3.back*amtToMove);
 JumpOn=false;
 }
 
 if(hitLEFT==true)
 {
 hitRIGHT=false;
 transform.Translate(Vector3.forward*amtToMove);
 JumpOn=false;
 }
 
 if (JumpOn==true)
 {
 transform.position.y -=20;
 }

if (JumpOn==true)
{
transform.position.y -=20;
JumpOn = false;
}

Also make sure that the conditions for jumping are not true again for the next frame.

You could fire a function from within your update if’s.

if (JumpOn==true)
 {
 JumpFunction();
 }

And then have the code in the function.

function JumpFunction(){
transform.position.y -=20;
 JumpOn = false;
}