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 dendens2 · Mar 06, 2013 at 02:10 AM · variablesswitch

How do I get two variables to switch places?

So I am working on my gun switch script, and I got it to work, except for one thing which causes it to only work once, and causes problems if you want to replace a gun. The problem that I am having is that I cannot get these two variables to switch. The variables are currentGun and secondaryGun. The commented out code causes problems in which the switching does not work correctly. Help!

 if(Input.GetButtonDown("Switch Guns"))
     {
         if(walkScript.currentGun == gameObject)
         {
             Debug.Log("Pressed Q");
             
             walkScript.currentGun.GetComponent(GunScript).carrying = true;
             walkScript.currentGun.GetComponent(GunScript).isHeld = false;
             
             walkScript.secondaryGun.GetComponent(GunScript).carrying = false;
             walkScript.secondaryGun.GetComponent(GunScript).isHeld = true;
             
             
             //walkScript.currentGun = walkScript.secondaryGun;
             
             //walkScript.secondaryGun = gameObject;
             
             
             
             
         
         }
         
     }
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 hoy_smallfry · Mar 06, 2013 at 03:14 AM

If both guns have this script attached, then chances are highly likely that when you press Q, both of them are firing off the switch script. So, one instance of the gun script will swap the two, but then the other instance swaps it back!

I don't suggest that have the gun switching code in the guns themselves. It's bad design, and as you can see already, it's gonna cause you trouble. Instead, take this script, modify it, and attach it to your character instead. The character is acting on the guns to switch them; I mean, would you find it weird in the real world if the guns switched themselves?

If you create a "Gun Switch" script that's attached to the player, you can then place currentGun and secondaryGun in it instead of the walk script, which, should only be for walking.

Additionally, I see you have two separate variables for "carrying" and "held". Instead, you could probably improve that by using only one variable. See, you can't hold something and carry it at the same time, so simply treat isHeld = true as being held, and isHeld = false as being carried.

This works perfectly if you are not adding any other states. If you do intend to add other states, try making an enumuration:

 public static var GunStatus
 {
     "isHeld" : 0,
     "carrying" : 1,
     "dropped" : 2
 };

Then, instead of isHeld and carrying variables, replace the two with one Number var called status, which can then be used like this:

 // don't use #pragma strict for this to work
 currentGun.status = GunStatus.isHeld;
 secondaryGun.status = GunStatus.carrying;

Lastly, in order to swap the variables, you just need to create a temporary one:

 var swapGun = currentGun;
 currentGun = secondaryGun;
 secondaryGun = swapGun;

Hope that helps!

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 dendens2 · Mar 06, 2013 at 03:25 AM 0
Share

Thanks you! :D By the way, the reason I am using both carrying and isHeld because I also pick up guns off the floor.

avatar image hoy_smallfry · Mar 06, 2013 at 03:27 AM 0
Share

Fair enough :)

avatar image
0

Answer by $$anonymous$$ · Mar 06, 2013 at 02:41 AM

You are going to have to create a temp variable like this:

 var tempVar = currentGun; //saves current gun to a temp var so its isn't overwritten
 currentGun = secondaryGun;
 secondaryGun = tempVar;
Comment
Add comment · Show 1 · 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 dendens2 · Mar 06, 2013 at 03:07 AM 0
Share

Thing is, I now am using this code:

     if(Input.GetButtonDown("Switch Guns"))
     {
     
         
         Debug.Log("Pressed Q");
             
         var tempVar: GameObject = walkScript.currentGun;
             
         walkScript.currentGun.GetComponent(GunScript).carrying = true;
         walkScript.currentGun.GetComponent(GunScript).isHeld = false;
             
         walkScript.secondaryGun.GetComponent(GunScript).carrying = false;
         walkScript.secondaryGun.GetComponent(GunScript).isHeld = true;
             
             
         walkScript.currentGun = walkScript.secondaryGun;
             
         walkScript.secondaryGun = tempVar;
             
             
             
             
         
         
         
     }


However, in the debug they are not switching, and I am having the same problem. :\

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

11 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

Related Questions

Best way to interchange variables of a single script? 1 Answer

Why my script isn't work? 1 Answer

Checking variable each frame to increase another variable every few seconds 1 Answer

Changing the shoot angle on one robot changes all of the other robots' variables? 1 Answer

Save & Loading Variables 2 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