- Home /
Gun Firing help?
Hi huys, I followed a guide on here about how to make a gun fire, and it gave me this javascript code:
var prefabBullet = gameObject;
function Update() { if(Input.GetButton("Fire1")){
bullet = Instantiate(prefabBullet; gameObject.Find("spawn").transform.position;
bullet.rigidbody.AddForce(transform.forward * Speed);
}
}
I have a prefab of a square gameObject named prefabBullet, but i keep getting an error saying that the code has an "unexpected token 'prefabBullet'". I'm not quite sure what it wrong here, so any help would be much appreciated thanks.
This cant be it, can you show me the guide you were using?
gameObject does not exist. What you see in that example is GameObject, and it is a type declaration. Sry im on phone, this is to slow to go in depth, so ill just suggest that you go over some basic progra$$anonymous$$g lessons.
Answer by Supercrusher9000 · May 31, 2014 at 11:30 PM
If that is the entire script than you need to have a variable for speed and set it to an int. I would suggest var speed = 5; and if it is not spawning in that is because the script is attempting to pull from your assets. So you have to make a gameobject call it spawn and then drag that gameobject into your assets folder.
Thanks. It's still saying that "prefabBullet" is an unexpected token. I have the "spawn" prefab in my assets already.
If it helps, http://answers.unity3d.com/questions/359665/how-to-make-a-gun-shoot-1.html is where i got it.
Alright I may have made a mistake, name the gameObject that you made prefabBullet
This is where I'm a little confused. It is named prefabBullet. and it is a prefab. So I don't see why the code isn't working?
Just to be on the safe ins$$anonymous$$d of having if(Input.GetButton("Fire1")){ have if(Input.Get$$anonymous$$eyDown("E")){, that's the only thing I think that could be wrong
Answer by Pkninja16 · Jun 01, 2014 at 02:38 AM
Did you make sure to check that your prefabBullet has an object in it? Check in the inspector and if the prefabBullet is empty, drag the gameobject that you want in there. This may or may not be the problem.
Your answer
Follow this Question
Related Questions
Need help... Start Button no longer working!!!!! 0 Answers
I need help with a firing script 1 Answer
ammo display javascript 1 Answer
My gun wont fire ! 1 Answer
How to let my gun shoot ?? 0 Answers