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 MrVopemz · Aug 11, 2013 at 12:52 AM · errorgunshoot

Shoot Script not working correctly

I was following this tutorial for a shoot script : LINK. I copied the script and I am having two errors. The first error is that the BulletsLeft doesnt go down when I shoot. The second error says:

NullReferenceException: Object reference not set to an instance of an object Shoot.Shot () (at Assets/Scripts/Shoot.js:71) Shoot.Update () (at Assets/Scripts/Shoot.js:47)

Line 71 is Debug.DrawRay(transform.posistion , DirectionRay * Range , Color.red);

Here is my Code

 var Range : float = 1000;
 var Force : float = 1000;
 var Clips : int = 2;
 var BulletsPerClip : int = 16;
 var ReloadTimer : float = 0;
 var ReloadCooler : float = 3.3;
 var Damage : int = 10;
 var BulletsLeft : int = 16;
 var ShootTimer : float = 0;
 var ShootCooler : float = 0.09;
 public var ShootSound : AudioClip;
 public var ReloadSound : AudioClip;
 public var EmptySound : AudioClip;
 var HitParticles : ParticleEmitter;
 function Start () 
 {
     BulletsLeft = BulletsPerClip;
     HitParticles.emit = false;
 }
 
 function Update ()
 {
     if(ReloadTimer > 0){
         ReloadTimer -= Time.deltaTime;
     }
     if(ReloadTimer < 0){
         ReloadTimer = 0;
     }
     
     if(ShootTimer > 0){
         ShootTimer -= Time.deltaTime;
     }
     if(ShootTimer < 0){
         ShootTimer = 0;
     }
     if(BulletsLeft < 0){
         BulletsLeft = 0;
     }
     if(Input.GetMouseButtonDown(0) && ShootTimer == 0)
     {
         if(BulletsLeft > 0)
         {
             if(ShootTimer == 0)
             {
                 PlayShootSound();
                 ShootTimer = ShootCooler;
                 Shot();
                 BulletsLeft --;
                 }
             }
             if(BulletsLeft == 0 && Clips == 0)
             {
                 if(ShootTimer == 0)
                 {
                     PlayEmptySound();
                     ShootTimer = ShootCooler;
                 }
             }
             if( BulletsLeft == 0 && Clips > 0 && ReloadTimer == 0)
             {
                 PlayReloadSound();
                 Reload();
             }
         }
     }
 function Shot ()
 {
 
     var Hit : RaycastHit;
     var DirectionRay = transform.TransformDirection(Vector3.forward);
     Debug.DrawRay(transform.posistion , DirectionRay * Range , Color.red);
     if(Physics.Raycast(transform.posistion , DirectionRay , Hit , Range))
     {
         if(Hit.rigidbody)
         {
             if(HitParticles)
             {
                 HitParticles.transform.posistion = Hit.point;
                 HitParticles.transform.localRotation = Quaternion.FromToRotation(Vector3.forward, Hit.normal);
                 HitParticles.Emit ();
                 Hit.rigidbody.AddForceAtPosition(DirectionRay * Force , Hit.point);
                 Hit.collider.SendMessageUpwards ("ApplyDamage" , Damage, SendMessageOptions.DontRequireReciever);
                 }
             }
         }
     }
 function Reload()
 {
     ReloadTimer = ReloadCooler;
     yield WaitForSeconds(ReloadTimer);
     BulletsLeft = BulletsPerClip;
     Clips -=1;
 }
 function PlayShootSound ()
 {
     audio.PlayOneShot (ShootSound);
 }
 function PlayReloadSound ()
 {
     audio.PlayOneShot (ReloadSound);
 }
 function PlayEmptySound ()
 {
     audio.PlayOneShot (EmptySound);
 }


Comment
Add comment · Show 2
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 ryan123321 · Aug 12, 2013 at 02:11 PM 0
Share

Try shooting the raycast before you debug.

avatar image ryan123321 · Aug 12, 2013 at 02:13 PM 0
Share

And in function shoot try BulletsLeft --; this will subtract 1 each time you shoot.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Joyrider · Aug 12, 2013 at 02:47 PM

Euh, you wrote posistion everywhere instead of position...

And on a sidenote, your if(ShootTimer == 0)-statements on line 43 and 53 will always be true, since you already checked this in the if-statement enclosing these: if(Input.GetMouseButtonDown(0) && ShootTimer == 0)

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.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

Multiple Cars not working 1 Answer

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

BCE0005: Unknown identifier: 'DirectionRay'. 1 Answer

Problem doing a script. 2 Answers

Error line 22! Help. i dont know why? 1 Answer


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