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 Borzi · Apr 15, 2014 at 08:03 PM · classinventoryintforeach

Problem with button press

Hello. I have a small problem at the moment. I think that the issue has to do with the processing speed of the code. The problem is this: When "ButtonPressed" is bigger than "CurWeapon", the switch does not take place immediately and the weapon that was switched from is still active while the new weapon is too. Whenever "ButtonPressed" is bigger than "CurWeapon", the switch takes place immediately. Anybody know how I could fix this?

Here is my code which does not work:

 void ChangeWeapon(int ButtonPressed, bool reset)
     {
      Debug.Log("Attempting to change weapon");
     HUD hud = transform.GetComponentInChildren<HUD>();
     bool didWeChange = false;
      bool canSwitch = false;
     foreach(Weapon wp in WeaponsList)
     {
     if(wp.WeaponKey == ButtonPressed)
     {
     Debug.Log("Button for " + wp.Name + " pressed");
         if(wp.Unlocked == true)
         {
         canSwitch = true;
         Debug.Log("Can switch!");
         }
     }
     if(wp.isCurWeapon == true && reset == false)
         {
         Debug.Log(wp.Name + " is being deactivated...");
         if(canSwitch == true)
             {
             wp.isCurWeapon = false;
                wp.ActualWeapon.gameObject.SetActive(false);
                Debug.Log("Deactivated " + wp.Name);
             }
         else
             {
             Debug.LogError("Cannot deactivate");
             }
         }
     else
         {
         Debug.Log(wp.Name + " is not the weapon that is being switched from!");
         }
     if(wp.WeaponKey == ButtonPressed && didWeChange == false)
        {
          if(wp.Unlocked == true)
           {
           Debug.Log("Changing weapon..");
           CurWeapon = ButtonPressed;
           Debug.Log(CurWeapon + " new CurWeapon");
           LastWeapon = WeaponPlaceHolder;
           Debug.Log(LastWeapon + " new Last Weapon");
           WeaponPlaceHolder = CurWeapon;
  
           wp.ActualWeapon.gameObject.SetActive(true);
           wp.isCurWeapon = true;
            
           Debug.Log("Activated " + wp.Name);
           
           hud.DisplayCurAmmo = wp.Ammo;
           hud.DisplayCurBulletsInClip = wp.BulletsLeft;
           hud.DisplayBulletsPerClip = wp.BulletsPerClip;
            hud.DisplayMaxAmmo = wp.MaxAmmo;
           
           if(wp.NeedsToReload == true)
             {
             NeedsToReload = true;
             }
           else
             {
             NeedsToReload = false;
             }
           didWeChange = true;
           break;
           Debug.Log("Weapon Changed");
           }
        }
     }
     }

Anyone know how I could tackle this?

Edit:

I updated the script sample and found out, that the problem is due to order of operations (check image). Notice how the "SMG is being deactivated..." debug is being called before the "Can switch!". This means that it actually checks what weapon is being deactivated for making sure if it can actually be deactivated. Anybody know how I can prevent this from happening? alt text

screen shot 2014-04-16 at 14.36.58.png (62.9 kB)
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
Best Answer

Answer by Radivarig · Apr 15, 2014 at 08:44 PM

In your second script you didn't use your wp in foreach and you change your weapon at the first instance where curWeapon is not equal to ButtonPressed, maybe that's why it acts very quick, by the way you change it (WeaponsList.Count -1) times, add a break after changing because once you changed it you don't need the rest of the checking. This is definitely faster but I don't know how your Weapon class looks like so I couldn't test it:

 void ChangeWeapon(int ButtonPressed)
 {
     if(CurWeapon == ButtonPressed)
         return;
     
     HUD hud = transform.GetComponentInChildren<HUD>();
     bool didWeChange = false;

     foreach(Weapon wp in WeaponsList)
     {
         if(didWeChange == false && CurWeapon == wp.WeaponKey )
         {
             if(wp.Unlocked == true)
             {
                 Debug.Log("Changing weapon..");
                 CurWeapon = ButtonPressed;
                 LastWeapon = WeaponPlaceHolder;
                 WeaponPlaceHolder = CurWeapon;
                 
                 wp.ActualWeapon.gameObject.SetActive(true);
                 wp.isCurWeapon = true;
                 
                 Debug.Log("Activated " + wp.Name);
                 
                 if(wp.NeedsToReload) 
                     hud.DisplayBulletsPerClip = wp.BulletsPerClip;
                 hud.DisplayCurAmmo = wp.Ammo;
                 hud.DisplayCurBulletsInClip = wp.BulletsLeft;

                 didWeChange = true;
                 Debug.Log("Weapon Changed");
             }
         }

         if(didWeChange && wp.WeaponKey == LastWeapon)
         {
             wp.isCurWeapon = false;
             wp.ActualWeapon.gameObject.SetActive(false);
             Debug.Log("Deactivated " + wp.Name);

             break; //we set new weapon and deactivate last one, now it's safe to break
         }
     }
 }
Comment
Add comment · Show 7 · 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 Borzi · Apr 15, 2014 at 08:53 PM 0
Share

This didn't really work 100%. The break actually prevents both weapons activating at the same time, but I need to click twice to change to the weapon I actually wanted to activate.

avatar image Radivarig · Apr 15, 2014 at 11:15 PM 0
Share

Wow I can't believe I completely missed the whole second part of the script, must be that I scrolled too far, sorry about that. Now I see what was happening there, you were calling recursively for each weapon to turn on or off, the normal SetActive should do just fine as it automatically does for all nested objects, and second most important thing was turning off even tho they were already turned off which took a lot of time since you didn't break anywhere. Check my updated answer, and tell me if it works for you

avatar image Radivarig · Apr 16, 2014 at 11:05 AM 0
Share

Should reset equal true ins$$anonymous$$d of false in line 9.? It does seem strange, since they are all called at the same place, what does debug say about current hud display values vs. CurWeapon values? Also, you can replace lines 37.-44. with NeedsToReload = wp.NeedsToReload;

avatar image Borzi · Apr 16, 2014 at 12:22 PM 0
Share

Actually the reason that you missed out on the second part is because I added it in later as I thought it might come in handy (sorry for that). Thanks for updating your answer though, I actually found out a lot of problems with the script. Check the question again, I updated it.

avatar image Borzi · Apr 17, 2014 at 07:56 AM 0
Share

Have your answer for effort, since it works now and I don't give 2 flying f***ks about anything else. Be aware that you didn't really suggest adding a new function at all. Hope it makes you happy :D I wish you a good life, sir.

Show more comments
avatar image
0

Answer by bigdaddy · Apr 15, 2014 at 08:58 PM

If I'm reading your code right, it looks like you want to loop over each weapon in the WeaponsList looking for the weapon that is unlocked and who's WeaponKey is the ButtonPressed.

The CurWeapon != ButtonPressed looks to be used to stop checking the WeaponKey if CurWeapon and ButtonPressed match; we can move that out of the loop and use the C# break command inside the loop to stop looping on a match

 public void ChangeWeapon(int ButtonPressed)
  {
     if (CurWeapon != ButtonPressed) 
     {
         foreach(Weapon wp in WeaponsList)
         {
            if(wp.Unlocked && wp.WeaponKey == ButtonPressed)
            {
                Debug.Log("Trying to change weapon...");
                CurWeapon = ButtonPressed;
                LastWeapon = WeaponPlaceHolder;
                WeaponPlaceHolder = CurWeapon
                Debug.Log("Weapon Changed");
                break;
            }
         }
     }    
  }
  
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 Borzi · Apr 16, 2014 at 02:55 PM 0
Share

Hey man, thanks for your reply. I updated the question, you could look at the question again if you want to.

avatar image bigdaddy · Apr 16, 2014 at 06:31 PM 0
Share

No worries, Radivarig had you covered

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

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

Related Questions

Inventory armor wielding proplem,How to convert from derived to base 1 Answer

How can i store the Amount of an item i have in an inventory? 0 Answers

C# Inventory System: Modify a targeted character's stats 1 Answer

getting values from class 1 Answer

an object reference is required to access non static member problem 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