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 Conect11 · Nov 14, 2013 at 12:13 AM · functioncycleweaponchanging

[SOLVED] Cycling all the way through weapons properly

Thanks to the amazing ardizzle for solving this! To me, working through this post is EXACTLY what this community is all about!

Edit: You know, I realize that maybe I'm asking the wrong question, and making this far too complicated. Let me ask this:

Is there a way to call a function multiple times in the same script? Right now, if I try I get a "StackOverflow" error.

The gist of my post below is I have three weapons. (According to the script, four, but one of them is simply an empty hand) By pressing the "Weapon Switching" button on the gamepad the weapon switches to the next weapon, ironically enough. The issue is that when I get to the final weapon it stays on that rather than cycling back to the first one (Empty hand) and starting the process over. So my latest thought is if I could just call the original function again (SelectWeapon, or even Update) in the script below, that might work. I can't believe how incredibly frustrated I am by this, it seems like it would be such a simple thing to do, but this issue has dogged me for a month now. [END OF EDIT]

Hello again,

I've been having some success using a weapon switching script, but need to cycle completely through the player's weapons. Right now player is able to cycle from 1 - 4, but then stays at weapon 4. I need them to be able to restart at 1 once they have 4 equipped, and have pressed the "weapon switching" controller button. I tried calling "Update" again, only to get a stack overflow. (ok, that makes sense, but was willing to try it) Any help is greatly, and humbly, appreciated. God bless.

 var Weapon01 : GameObject;
 var Weapon02 : GameObject;
 var Weapon03 : GameObject;
 var Weapon04 : GameObject;
 static var weaponready = false;
 static var swordready = false;
 static var cameraready = false;
 
 
 
 function Update () {
 
 if (Input.GetButtonDown("Weapon Switch")) {
 if (weaponready == true)
 SelectWeapon();
 }    
 }
     
     
     
     
 
     function SelectWeapon () {
     if (Weapon01.active == true)
     
     {
     if (WeaponInv.Club >=1)
     Weapon01.SetActiveRecursively(false);
     Weapon02.SetActiveRecursively(true);
     Weapon03.SetActiveRecursively(false);
     Weapon04.SetActiveRecursively(false);
     }
     if (Input.GetButtonDown("Weapon Switch")) {
     if (WeaponInv.Sword >=1)
     if (swordready == true)
     SwordSelect();
     }
     }
     
         function SwordSelect () {
         if (Weapon02.active == true)
         {
         if (WeaponInv.Sword >=1)
         if (swordready == true)
         Weapon01.SetActiveRecursively(false);
         Weapon02.SetActiveRecursively(false);
         Weapon03.SetActiveRecursively(true);
         Weapon04.SetActiveRecursively(false);
         }
         if (Input.GetButtonDown("Weapon Switch")) {
         if (cameraready == true)
         CameraSelect();
     }
     }
     
     
     
                     function CameraSelect () {
                     if (Weapon03.active == true)
                     {
                     if (WeaponInv.Camera >=1)
                     if (cameraready == true)
                    
         
                     Weapon01.SetActiveRecursively(false);
                     Weapon02.SetActiveRecursively(false);
                     Weapon03.SetActiveRecursively(false);
                     Weapon04.SetActiveRecursively(true);
                     }
                     if (Input.GetButtonDown("Weapon Switch")) {
                     Update();
                     }
                     }
Comment
Add comment · Show 4
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 Conect11 · Nov 14, 2013 at 02:32 AM 0
Share

darn it. Tried "invokerepeating" too, hoping that would work. Nein.

avatar image flamy · Nov 14, 2013 at 02:58 AM 0
Share

First of all y not have a list or array list or array for the weapons...

avatar image Conect11 · Nov 14, 2013 at 03:01 AM 0
Share

the array thing usually comes back to bite me in the butt, lol. The long and short of it is that I was having a heck of a time saving arrays to playerprefs. (and even playerprefsx)

avatar image Huacanacha · Nov 15, 2013 at 05:35 AM 0
Share

Any reason you don't have an array of GameObjects and use and int as the index? Then you can just update switch to the next weapon with "wIndex = (wIndex+1)%4;" which will handle the looping back to 0. To set active you can use a for loop and deter$$anonymous$$e if the current weapon should be active using "wIndex == i".

1 Reply

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

Answer by ardizzle · Nov 14, 2013 at 03:58 AM

Ok man. I wrote you up a script that should work. You might have to make some small changes to make it work for your project. I just made 4 game objects and made it turn them on and off in an order. Here it is :

 #pragma strict
 
 var Weapon01 : GameObject;
 var Weapon02 : GameObject;
 var Weapon03 : GameObject;
 var Weapon04 : GameObject;
 // Check to see if you have these weapons yet.
 var obtainedWeapon03 : boolean = false;
 var obtainedWeapon04 : boolean = false;
 static var weaponready = true;
 static var swordready = false;
 static var cameraready = false;
 static var equipSlot : int = 1;
 
 function Start()
 {
     WeaponsFalse(); // Makes sure you don't have more than one weapon set.
     Weapon01.SetActive(true);// Makes sure you start the game with your first weapon
 }
 function Update () {
     if(Input.GetMouseButtonUp(0))// You will have to change to what you need 
     {
         if(weaponready == true)
         {
             WeaponChange();
         }
     }    
 }
     function WeaponChange()
     {
         if(Weapon01.activeSelf == true)
         {
             // Changes to the second weapon
             WeaponsFalse();
                 Weapon02.SetActive(true);
                Debug.Log ("Second Weapon");
         }
     
         else if(Weapon02.activeSelf == true)
         {
             // If you have the 3rd weapon it switches else goes back to 1st weapon
             if(obtainedWeapon03 == true)
             {
                 WeaponsFalse();
                 Weapon03.SetActive(true);
                 Debug.Log ("Third Weapon");
             }
             else
             {
                 Debug.Log("Sorry you only have 2 weapons");
                 WeaponsFalse();
                 Weapon01.SetActive(true);
             }
         }
     
         else if(Weapon03.activeSelf == true)
         {
             // If you have the 4rd weapon it switches else goes back to 1st weapon
             if(obtainedWeapon04 == true)
             {
                 WeaponsFalse();
                 Weapon04.SetActive(true);
                 Debug.Log ("Fourth Weapon");
             }
             else
             {
                 Debug.Log("Sorry you only have 3 weapons");
                 WeaponsFalse();
                 Weapon01.SetActive(true);
             }
         }
     
         else if(Weapon04.activeSelf == true)
         {
             // Goes back to the 1st weapon
             WeaponsFalse();
             Weapon01.SetActive(true);
             Debug.Log ("First Weapon");
         }
     }
 
     function WeaponsFalse()
     {
         // Deactivates all weapons to avoid having more than one weapon active at once
         Weapon01.SetActive(false);
         Weapon02.SetActive(false);
         Weapon03.SetActive(false);
         Weapon04.SetActive(false);
     }

I also added some variables so that if you don't have the 3rd weapon or the 4th weapon it will set you back to the first weapon. What do you think?

Comment
Add comment · Show 20 · 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 Conect11 · Nov 14, 2013 at 04:05 AM 0
Share

had thought of that, but when I tried to implement it, nothing happened. Actually, worse than nothing, it blocked weapon 3. :(

Dang, wish I had saved that version ($$anonymous$$ono crashed) so I could show what I did.

avatar image ardizzle · Nov 14, 2013 at 04:29 AM 0
Share

ok then try something like this

 function WeaponChange()
 {
      if(Weapon01.active == true)
      {
          WeaponsFalse();
          Weapon02.active = true;
      }
 
      else if(Weapon02.active == true)
      {
          WeaponsFalse();
          Weapon03.active = true;
      }
 
      else if(Weapon03.active == true)
      {
          WeaponsFalse();
          Weapon04.active = true;
      }
      
      else if(Weapon04.active == true)
      {
          WeaponsFalse();
          Weapon01.active = true;
      }
 }
 
 function WeaponsFalse()
 {
       Weapon01.SetActiveRecursively(false);
       Weapon02.SetActiveRecursively(false);
       Weapon03.SetActiveRecursively(false);
       Weapon04.SetActiveRecursively(false);
 }
 

I think something along those lines should work and make your code shorter.

avatar image Conect11 · Nov 14, 2013 at 09:17 PM 0
Share

hey ardizzle, thanks for that. Still trying to utilize what you've got there with what I'm trying to do, but running into snags. Unfortunately, the way I have it set up right now doesn't allow me to change to ANY weapons. Still giving it the college try :) Posted below:

 var Weapon01 : GameObject;
 var Weapon02 : GameObject;
 var Weapon03 : GameObject;
 var Weapon04 : GameObject;
 static var weaponready = false;
 static var swordready = false;
 static var cameraready = false;
 static var equipSlot : int = 1;
 
 function Update () {
     if (Input.GetButtonDown("Weapon Switch")) {
     Debug.Log ("script firing");
         if (weaponready == true)
             WeaponChange();
     }    
 }
     function WeaponChange()
     {
     if(Weapon01.active == true)
     {
     WeaponsFalse();
     Weapon02.active = true;
     }
      
     else if(Weapon02.active == true)
     {
     WeaponsFalse();
     Weapon03.active = true;
     }
      
     else if(Weapon03.active == true)
     {
     WeaponsFalse();
     Weapon04.active = true;
     }
      
     else if(Weapon04.active == true)
     {
     WeaponsFalse();
     Weapon01.active = true;
     }
     }
      
     function WeaponsFalse()
     {
     Weapon01.SetActiveRecursively(false);
     Weapon02.SetActiveRecursively(false);
     Weapon03.SetActiveRecursively(false);
     Weapon04.SetActiveRecursively(true);
     }
avatar image ardizzle · Nov 16, 2013 at 12:35 AM 1
Share

Edited my original answer and put in the new script.

avatar image ardizzle · Nov 16, 2013 at 01:07 PM 1
Share

No problem. I had many people go above and beyond for me. Also if you ever need to hire an extra programer I do freelance work. You can email me at ardizzle@gmail.com

Show more comments

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

18 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

Related Questions

Trying to cycle through weapons 1 Answer

Cant call function in js another script 1 Answer

Cutscene Segments 1 Answer

Object will not instantiate 1 Answer

Unities Javascript, declaring and using a non-generic function 3 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