• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by TheWickedDead · May 31, 2014 at 11:24 PM · javascriptfpsgunfire

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.

Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Lovrenc · May 31, 2014 at 11:26 PM 0
Share

Please post entire script.

avatar image TheWickedDead · May 31, 2014 at 11:27 PM 0
Share

That is the entire script.

avatar image Lovrenc · May 31, 2014 at 11:36 PM 0
Share

This cant be it, can you show me the guide you were using?

avatar image TheWickedDead · May 31, 2014 at 11:37 PM 0
Share

http://answers.unity3d.com/questions/359665/how-to-make-a-gun-shoot-1.html

avatar image Lovrenc · May 31, 2014 at 11:45 PM 0
Share

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 programing lessons.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TheWickedDead · May 31, 2014 at 11:32 PM 0
Share

Thanks. It's still saying that "prefabBullet" is an unexpected token. I have the "spawn" prefab in my assets already.

avatar image TheWickedDead · May 31, 2014 at 11:37 PM 0
Share

If it helps, http://answers.unity3d.com/questions/359665/how-to-make-a-gun-shoot-1.html is where i got it.

avatar image Supercrusher9000 · May 31, 2014 at 11:39 PM 0
Share

Alright I may have made a mistake, name the gameObject that you made prefabBullet

avatar image TheWickedDead · May 31, 2014 at 11:40 PM 0
Share

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?

avatar image Supercrusher9000 · May 31, 2014 at 11:52 PM 0
Share

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

Show more comments
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Shooting Pistol and Machine Gun 2 Answers

Is there a script out there that actully works for shooting bullets? 2 Answers

Help with fire script 1 Answer

Need coding help with fps 0 Answers

transform code! 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges