- Home /
instantiate not working properly (javascript)
Hi. I am working through some tutorials and getting to grips with scripting. I have created a simple script to instantiate an object to fire at a wall of blocks i have built. the problem is that as soon as i press play to test the game it is already instantiating the object before i press the mouse button(fire1). Could someone please look at the script and tell me if there is something wrong with it. Thank you.
var bullet : Rigidbody; var power : float = 1500; var moveSpeed : float = 5;
function Update (){
var h : float = Input.GetAxis("Horizontal") * Time.deltaTime *
moveSpeed;
var v : float = Input.GetAxis("Vertical") * Time.deltaTime *
moveSpeed;
transform.Translate(h, v, 0);
if(Input.GetButtonUp("Fire1"));
var instance: Rigidbody = Instantiate(bullet, transform.position,transform.rotation);
var fwd: Vector3 = transform.TransformDirection(Vector3.forward);
instance.AddForce(fwd * power);
}
In the future, please format your code correctly. Watch the tutorial video if you don't know how.
Answer by amphoterik · Jul 16, 2013 at 12:59 PM
Hello. You need to remove the semi-colin (;) from your code. This:
if(Input.GetButtonUp("Fire1"));
Needs to be this:
if(Input.GetButtonUp("Fire1"))
The semi colon ends the IF statement without doing anything. This is syntactically correct, but logically an error.
Thank you amphoterik works great now. i spent all morning going through answers on here and didnt even notice the semi-colon at the end. thanks again and sorry about the format. it will be done properly the next time i need to post a question.