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 Tony 5 · Apr 13, 2011 at 05:16 PM · javascriptfpsgun

Need help with bullet/gun scripting

Hi!! Im new to unity. I have my gun script, spawn point, bullet, and gun. heres my script.

var Bullet: Transform; function Update () { if (Input.GetButtonDown ("Fire1")){

var bullet=Instantiate(Bullet,GameObject.Find("Gun_Spawn").transform.position,Quaternion.identity); bullet.rigidbody.AddForce(transform.forward *2000);

}

} but every time I hit Fire1 (Left mouse) the bar at the bottom of the screen says, Theres no rigidbody attached to clone, script still searching?? I dont know what to do?? and I also need a script to destroy the bullet. Can any body help?? Thank you for the time.

Comment
Add comment · Show 1
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 AngryOldMan · Apr 13, 2011 at 05:21 PM 0
Share

one question per post but in answer to your destroy question http://lmgtfy.com/?q=unity3d+destroy+gameobject

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Marnix · Apr 13, 2011 at 05:22 PM

You are saying: bullet.rigidbody. But the bullet doesn't have one. Unity3D throws you a NullReferenceException. To solve this: add a new RigidBody component.

If your bullet is a prefab, you can just add it in your editor.

If it is a class that you are creating from code somehow, use a script:

bullet.addComponent(new RigidBody());

will probably work.

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 AngryOldMan · Apr 13, 2011 at 05:19 PM

well logic would dictate that you should add a rigidbody component to the prefab that your script is trying to instantiate

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 Kourosh · Apr 13, 2011 at 05:23 PM

The problem is that your bullet prefab does not have rigidbody attached.

just to remind you also in unity variables should be started with small letters.

Comment
Add comment · Show 2 · 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 AngryOldMan · Apr 13, 2011 at 05:41 PM 0
Share

why should unity variables be named with small letters? I have loads of codes with variables with a number of case differences in them and it doesnt make a difference to how they perform

avatar image Kourosh · Apr 13, 2011 at 06:04 PM 0
Share

Sorry i meant it's better to start variables with small letter. It's not that it won't work if you use Upper case, it's just a scripting discipline so they are not mistaken with functions which are usually started with Upper case. here is what i found: http://forum.unity3d.com/threads/6903-Is-there-a-recommended-na$$anonymous$$g-convention

and also wherever i checked in unity's scripting manual you'll see the same rule being applied.

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

No one has followed this question yet.

Related Questions

CS0116 "A namespace can only contain type and namespace declarations" 1 Answer

Camera Recoil,Please help me. 2 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Some issues with weapon switching and reloading? 2 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