Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 msassman · Feb 25, 2012 at 11:59 PM · javascriptbulletgungunfire

how to create a gun in unity3d

can someone please help im trying to make a gun shoot but it says NullRefenceExeption for this line

var bullet = Instantiate(BulletPrefab,

PLEASE HELP ME

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Hybris · Feb 26, 2012 at 08:44 AM

Hello massman,

I have problems with it as well, Unity is a little weird the code should be for example:

 var bullet = Instantiate(BulletPrefab, transform.position, transform.rotation);

If that doesn't work you have to do it the long way around:

 var bullet : Rigidbody;
 bullet = Instantiate(BulletPrefab, transform.position, transform.rotation);

I don't know if you already added a force to the bullet but here is the code(fast and short):

 bullet.rigidbody.AddForce(//force and stuff);

but I dont like this method I like to use transform.TransformDirection:

 bullet.transform.TransformDirection(Vector3.forward) * Force;
 //force being you force variable or just a number.

When you are using Vector3.forward, remember Vector3.forward is 0,0,1. Keep that in mind when multiplying.

Grz, Hybris

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
avatar image
0

Answer by scipiothegreat · Feb 27, 2012 at 02:24 AM

If that is the entire line, then it just needs to be completed. At the top of the script, outside of any functions, are where variables are defined. In this area, you should add something like

 var bulletPrefab : gameObject;

In the Inspector drag the prefab you want from Project View onto the empty variable. To launch it, you need to instantiate it, and then give it a velocity, assuming it has a rigidbody attached.

 var bullet = Instantiate(bulletPrefab, transform.position, transform.rotation);
 bullet.rigidbody.AddForce(bullet.transform.forward*100);
Comment
Add comment · Show 1 · 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 Hybris · Feb 27, 2012 at 07:04 PM 0
Share

I could be wrong but, I think transform.forward doesnt exist, and that you dont have to include the bullet part.

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.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Bullets will not fire forwards 1 Answer

Need help with my gun script! 1 Answer

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

How do I modify my script to give my gun recoil when I shoot? 1 Answer

how to make a first person controller shooting a target? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges