• 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 ToxicNova · Apr 08, 2015 at 11:50 PM · javascripterrorshootingunity 4.6disappearing

My gun can't shoot after some adjustments to the script

So I've asked a simmer question like this before, but the cause of this one (I know for a fact) is different from this one.

Anyway, I wanted to keep track off how much ammo I have on my gun so I made a few variables and added of few stuff to the Function update. once those parts were added, it made my gun not fire. Here is the script: #pragma strict

 var bullet : GameObject;
 private var bulletsPerSecond = 20.0;
 private var shooting = false;
 private var Allammo : int = 320;
 var ammo : int = 40;
 var sound: AudioClip; 
 var Gun : GameObject;
 var Subammo : int = 1;
 function Start()
 {
     InvokeRepeating("Shoot", 0.0, 1.0 / bulletsPerSecond);
 }
 function Shoot()
 {
     if (!shooting) return;
     var go = Instantiate(bullet, transform.position, transform.rotation);
     go.rigidbody.AddRelativeForce(Vector3.forward * 1000.0);
     ammo = (ammo - 1);
     audio.PlayOneShot(sound);
     SendMessage("Ammo_Subtract", Subammo, SendMessageOptions.DontRequireReceiver);
 }
 function Update()
 {
     shooting = false;
     if(Input.GetAxis("Fire1"))
     {
         shooting = true;
     }
     if (ammo == 0)
     {
         Allammo = (Allammo - 40);
         ammo = 40;
         SendMessage("All_ammoSub", ammo, SendMessageOptions.DontRequireReceiver);
     }
     if (Allammo == 0);
     {
         shooting = false;
     }
     
 }

This is on Unity 4.6, and these are the added parts that caused it not to shoot:

 if (ammo == 0)
     {
         Allammo = (Allammo - 40);
         ammo = 40;
         SendMessage("All_ammoSub", ammo, SendMessageOptions.DontRequireReceiver);
     }
     if (Allammo == 0);
     {
         shooting = false;
     }

Ignore the SendMessage part of that (unless that is affecting it which i highly doubt). Anyway, if you find anything wrong with the script, please let me know, thanks!

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 ToxicNova · Apr 08, 2015 at 11:58 PM 0
Share

i think the-

  if (Allammo == 0);
      {
          shooting = false;
      }

is making the problem but don't know for sure

1 Reply

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

Answer by DoTA_KAMIKADzE · Apr 09, 2015 at 12:01 AM

This part (line 25):

 Input.GetAxis("Fire1")

Returns float value and so it can't be used in if statement and also it should give you an error...change it to something like:

 if(Input.GetButtonDown("Fire1"))

P.S. Though if it is an axis not a button then check something like:

 if(Input.GetAxis("Fire1") > 0)

That will work.

P.P.S. You can also just check if it "!= 0" if any direction is fine for fire.

P.P.P.S. Though your code is still pretty badly designed for your repeating function, how about rewriting it like this?:

     if (Allammo > 0)
     {
         if (ammo > 0)
         {
             if (Input.GetButtonDown("Fire1"))
             {
                 shooting = true;
             }
             if (Input.GetButtonUp("Fire1"))
             {
                 shooting = false;
             }
         }
         else
         {
             shooting = false;
             Allammo = (Allammo - 40);
             ammo = 40;
             SendMessage("All_ammoSub", ammo, SendMessageOptions.DontRequireReceiver);
         }
     }
     else shooting = false;

I think that will suit you more.

P.P.P.P.S. Ah yeah also completely forgot:

Change line #15 to:

 if (!shooting || ammo < 1) return;

Just because your FPS might be low to prevent few more bullets going off ))

Comment
Add comment · Show 3 · 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 ToxicNova · Apr 09, 2015 at 12:04 AM 0
Share

I edited the script like you said but nothing changed, no sound playing, no bullets firing.

P.S. I've added and tried everything you have said but still getting the same problem :(

avatar image DoTA_KAMIKADzE · Apr 09, 2015 at 12:19 AM 0
Share

Remove all your code under Update function and paste the one in my "P.P.P.S." section I guess that is the behavior you expect, if not then let me know.

avatar image ToxicNova · Apr 10, 2015 at 04:51 PM 0
Share

That did it, thank you!

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

19 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

Related Questions

im having trouble with raycast shooting script 0 Answers

Cannot add or drag an asset to the inspector 2 Answers

fps shooting in the direction of character main cam 1 Answer

Shooting question 1 Answer

WebGL on Firefox Linux displays InvalidStateError,WebGL on Linux Firefox displays InvalidStateError 0 Answers

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