Super noob scripting question

Hi,I’ve script that instantiate an object,but i need it to create one object per time,because now when I press de fire1 button it creates a lot of objects.here is my code:

var speed=0.1;
var build:GameObject;

function Update () {
 var x=Input.GetAxis("Horizontal")*speed;
 var z=Input.GetAxis("Vertical")*speed;
 transform.Translate(x,0,z);
 if(Input.GetAxis("Fire1"))
 {
 var instance:GameObject=Instantiate(build,transform.position,transform.rotation);
 }
 }

What I need to do this?(one instantiation per time)
Another question,how can I attach a prefab in code?For example,here:

var build:GameObject;

I’ll need to set the var build prefab in the editor,but how can I set this in code?
And more one question;
Sorry for my bad English?

Most likely a Wait for seconds with a function. In that function you would have the instantiate and a wait for seconds and call that function in place of the instantiate you have right now.

var speed=0.1;
var build:GameObject;

function Update () {
 var x=Input.GetAxis("Horizontal")*speed;
 var z=Input.GetAxis("Vertical")*speed;
 transform.Translate(x,0,z);
 if(Input.GetAxis("Fire1"))
 {
Build();
}
 }
 function Build()
 {
  var instance:GameObject=Instantiate(build,transform.position,transform.rotation);
  WaitForSeconds(50);
 }

Like this?Doesn’t work:(

put the: WaitForSeconds(50); in front of the: var instance:GameObject=Instantiate(build,transform.position,transform.rotation);

The same thing happens:(

are you using C# or JavaScript/UnityScript? It appears you are using JavaScript. Use this:

var speed = 0.1;
var build : GameObject;

function Update () {
 var x=Input.GetAxis("Horizontal")*speed;
 var z=Input.GetAxis("Vertical")*speed;
 transform.Translate(x,0,z);
 if(Input.GetButtonDown("Fire1"))
 {
Build();
}
 }
 function Build()
 {
  var instance:GameObject=Instantiate(build,transform.position,transform.rotation);
 }

that is one time per click, instead of multiple times.

Yes it works!Thank so much:smile:

ok, your welcome

Just as a heads up, if you ever DO want to use WaitForSeconds(), you can only use it as a yield instruction in a coroutine, you can’t just bust it out in Update().

It seems you already have your answer but if you did want to fire periodically whilst holding down the fire button you can do so by using the Input.GetAxis(“Fire1”) and recording the time that you instantiate your prefab into a private global variable . Add an if statement before the instatiate that compares the difference between the last time the prefab was instatiated and the current against a shootSpeed variable you declare in your class and set in the inspector. If the difference is greater than the shootSpeed then instatiate

This uTube tutorial by TornadoTwins tackles the problem you have observed in a somewhat round about way.