Instantiate only one prefab

Ok i have this script:

#pragma strict

         var moon : Transform;
 private var moonPosition;
         var spaceClicks;
         
         var rocketPosX;
         var rocketPosY;
         
         var distance = 100;
         var spawnMoon = false;
function Start () {
//rocketPosY = rocketPosY + distance;


}
 
 function Update () {
rocketPosX  = gameObject.Find("raket").transform.position.x; 
rocketPosY  = gameObject.Find("raket").transform.position.y + 10; 
spaceClicks = gameObject.Find("raket").GetComponent(RocketScript).clicks;

 
     
      if(spaceClicks == 5) 
      Instantiate(moon, Vector2(rocketPosX,rocketPosY), Quaternion.identity);
    }

 
In another script, when i press space, a rocket goes up one click. When i press it 5 times a mooon needs to be spawned. With this script i made, it spawns, but it spawns one every frame! How can instantiate only one moon?

Cant you add an and to your spaceclicks if statement which checks if teh moon object has already been spawned?
If not spawn it else dont.

First of try not to use .Find process as much as possible. you can find it once at start up.
in this way you will cache the transform which gonna boost you game.

 var rocket : Transform;
 
 function Start()
 {
	rocket = GameObject.Find("raket").transform;
 }
 
 rocketPosX  = rocket.position.x; 
 rocketPosY  = rocket.position.y + 10; 

and finally you can stop spawning each frame by resetting spaceClicks value to zero after each time it reached 5
So :

if(spaceClicks == 5)
{
	spaceClicks = 0;
	Instantiate(moon, Vector2(rocketPosX,rocketPosY), Quaternion.identity);
}

You could try checking for weather a moon has been spawned already, and if so, don’t do anything.

private GameObject currentMoon;

if(spaceClicks == 5 && currentMoon == null) 
{
     currentMoon = (GameObject) Instantiate(moon, Vector2(rocketPosX,rocketPosY), Quaternion.identity);
}