• 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 leonalchemist · Jan 31, 2013 at 08:40 PM · fpsgunammo

skip guns with no ammo left

similar to Quake 3 if i remember, when u scroll to change weapon and if that weapon has no ammo left, u automatically go to the next weapon with ammo available, my way doesn't fully work so how do i go about this; example of my setup is like this:

     if (Input.GetAxis("Mouse ScrollWheel") < 0)
     {
         if(GunNumber == 4 && GunArray[0].GetComponent(GunScriptNetwork).ExtraAmmo > 0 &&
         GunArray[0].GetComponent(GunScriptNetwork).AmmoInCurrentClip > 0)
         {
             GunArray[GunNumber].GetComponent(GunScriptNetwork).enabled = false;
             GunArray[0].GetComponent(GunScriptNetwork).enabled = true;

             GunArray[GunNumber].GetComponentInChildren(MeshRenderer).enabled = false;
             GunArray[0].GetComponentInChildren(MeshRenderer).enabled = true;
             GunNumber = 0;
         }

as u can see im disabling just the mesh and a script to change guns, i cant disable the entire object otherwise i cant pick up ammo and such; in this case all the script is doing is when the array reaches the 5th and last gun as i scroll down, it changes to the 1st one. need help plz

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

1 Reply

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

Answer by weenchehawk · Feb 02, 2013 at 01:11 AM

I'm not sure what you're doing with everything there, but I'd try something like

 // imagine you have a class variable called CurrentGun
 
 void Update()
 {
     int LastGunUsed = CurrentGun;
 
     if (Input.GetAxis("Mouse ScrollWheel") < 0)
     {
         bool FoundLoadedGun = false;
 
         while (!FoundLoadedGun)
         {
             // Select the next gun in the array to examine
             int SelectedGun = CurrentGun+1;
             
             // If it's the last gun in the array, loop back to the starting one
             if (SelectedGun = GunArray.Length)
                 SelectedGun = 0;
 
             // If the selected gun has ammo, set FoundLoadedGun = true and update CurrentGun 
             if (GunArray[SelectedGun].GetComponent(GunScriptNetwork).AmmoInCurrentClip > 0)
             {
                 FoundLoadedGun = true;
                 CurrentGun = SelectedGun;
             }
 
             // If we've run througfh the cycle so that we landed back on our CurrentGun again, we have no guns with ammo
             // you possibly want to default to knife/chainsaw/golf club/fists here/bad language (weapon 0??)
             if (SelectedGun == CurrentGun )
             {
                 FoundLoadedGun = true;
                 CurrentGun = 0;
             }
 
             // try the next gun
             SelectedGun++;
         }
     }
 
     GunArray[LastGunUsed].GetComponent(GunScriptNetwork).enabled = false;
     GunArray[LastGunUsed].GetComponentInChildren(MeshRenderer).enabled = false;
 
     GunArray[CurrentGun].GetComponent(GunScriptNetwork).enabled = true;
     GunArray[CurrentGun].GetComponentInChildren(MeshRenderer).enabled = true;
 }
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 leonalchemist · Oct 16, 2013 at 04:08 AM 0
Share

Loooooong overdue reply but after quite a lot of tweaking and fixing using your help; script now seem to work rly well; thx :)

avatar image weenchehawk · Aug 01, 2014 at 08:29 PM 0
Share

just noticed this reply now - no worries, glad it worked out.

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

10 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

Related Questions

how do i attach a gun to my character 2 Answers

ammo display javascript 1 Answer

Projectile Firing/Gun script 1 Answer

FPS UI ammo display problem 0 Answers

how do i make a ammo and reload system 1 Answer

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