Beginner problem- Instantiate

Hi! I’m going through the introductory tutorials and every time I fire multiple(about10) instances are created . Here is the code from the tutorial as I inputed. Am I missing something? - paul

var newObject : Transform;

function Update () {
if (Input.GetButton(“Fire1”)) {
Instantiate(newObject, transform.position, transform.rotation);
}

}

Update is called once per frame. If the game is running fast, the mouse will be down for more than one of the frames. You should probably instantiate directly in response to the OnMouseDown or OnMouseUp. It might look something like this:

function OnMouseDown () { 
   if (Input.GetButton("Fire1")) { 
      Instantiate(newObject, transform.position, transform.rotation); 
   } 
}

Try this:

public var newObject : Transform; 
public var createRate = 0.5;
private var lastShot = -10.0;

function Update ()
{ 
     if (Input.GetButton("Fire1"))
     {
          if (Time.time > lastShot + createRate)
          {
               Instantiate(newObject, transform.position, transform.rotation);
               lastShot = Time.time;
          }
     }
}

The simplest answer :wink: : Unless you want autofire, use GetButtonDown instead of GetButton.

–Eric

Thanks! I tried them all. I guess creating scripts can be as creative and varied as the artwork they define.

newObject : Rigidbody

try that…