Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 MrSplosion · Jun 03, 2011 at 12:21 AM · weaponswitchswitch objects

Weapon Switching

I'm simply trying to switch between two weapon game objects when you press a key. The script prints out "active" and "nonactive" so I know it works, it just doesn't activate and deactivate the game objects properly, and I have no idea why. Can someone help me out on this?

Here's the script:

 function Update(){
 if(Input.GetButtonDown("SwitchWeapons")){
     gun = GameObject.FindWithTag("gun");
     
     if(gun.active == true){
         gun.SetActiveRecursively(false); //empty and child objects
         print("active");
     }
     else if(gun.active == false){
         gun.SetActiveRecursively(true); //empty and child objects
         print("nonactive");
     }
 }

}

Thanks!

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
1
Best Answer

Answer by Talon · Jun 03, 2011 at 12:43 AM

Try assigning the guns to variables at the start of your script. I'd bet the way that you're finding the game objects is what is making your script work incorrectly. If you had a "for" function in there it might work, but an easier way would be to just define your gun game objects to separate variables at the start of your script Something like

 var gun1 : GameObject;
 var gun2 : GameObject;
 
 function Update(){
 if(Input.GetButtonDown("SwitchWeapons"))
   SwapWeapons();
 }
 
 function SwapWeapons(){
 if (gun1.active == true) {
 gun1.SetActiveRecursively(false);
 gun2.SetActiveRecursively(true);
 } else {
 gun1.SetActiveRecursively(true);
 gun2.SetActiveRecursively(false);
 }
 }
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 MrSplosion · Jun 03, 2011 at 10:27 PM 0
Share

Thanks it works, but the only problem is when the player picks up and swaps out weapons for other weapons those variables(gun1 and gun2) will have to change. So is it possible to do this with scripting?

avatar image LightSource · Sep 29, 2012 at 03:05 AM 0
Share

You could try assigining each gun a tag and making a seperate script to pull them from inventory.

avatar image
1

Answer by Talon · Jun 04, 2011 at 01:41 AM

just make the variables swap out when you pick up the new weapons as well. I'm not sure exactly how you're doing your pick up system, but if I was doing it I'd have the pick up item send a message to the player if they collide/interact with it that contains the gameobject information.

If you used something along these lines it'd should. Of course it'd be on collision or when it's activated

     var prefabToEnable : GameObject;
     var player : GameObject;
 
 function Start () {
     if (player == null) {
     gameObject.find("player");
     }
     }
 
 function PlayerPicksUpThisWeapon () {
     player.SendMessage("PickupWeapon", prefabToEnable, SendMessageOptions.DontRequireReceiver);
 }

then on your player gun script you'd make a new function that would add the new weapon in.

 function PickupWeapon (pickedupWeapon : GameObject) {
 if (gun1.active == true) {
 gun1 = pickedupWeapon;
 }
 if (gun2.active == true) {
 gun2 = pickedupWeapon;
 }


On the other hand you could go look at the dastardly banana FPS package and see how he did it. It is a pretty solid package and a great base to start from if you're trying to do a FPS game

http://www.dastardlybanana.com/FPSConstructorWeapons.htm

Comment
Add comment · 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

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Weapon pick up and switching script 2 Answers

Weapon Switching 1 Answer

Stop player being launched on switch weapons 1 Answer

Weapon swap help 1 Answer

Weapon Switching 1 Answer


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