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 /
This question was closed Oct 19, 2015 at 11:45 PM by ZachAttack9280 for the following reason:

The question is answered, right answer was accepted.

avatar image
1
Question by ZachAttack9280 · Oct 19, 2015 at 04:21 AM · javascriptweaponswitching

Weapon Switching

Hi, so for my game, i have two weapons or tools so far, a pickaxe and an axe, but i want to be able to change them by hiding one and showing one.

Here is my code:

 #pragma strict
  
  var axe : GameObject;
  var pickaxe : GameObject;
 private var visible : GameObject;
 private var enabled : GameObject;
 private var hide : GameObject;
  
  axe.active = true;
  pickaxe.active = false;
  
  function Update () {
      if (Input.GetKeyDown (KeyCode.Z))
      {
          hide = GameObject.Find("axe");
          hide.GetComponent.<Renderer>().enabled = false;
      }
      if (Input.GetKeyDown (KeyCode.X))
      {
          hide = GameObject.Find("axe");
          hide.GetComponent.<Renderer>().enabled = false;
  }
  }
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

  • Sort: 
avatar image
1
Best Answer

Answer by Whiteleaf · Oct 19, 2015 at 04:54 PM

Switch statements are the way to go for weapon switching. Here's what I do when I make weapon switching:

i make a script called "EquippedWeapons", or something related. I attach to the player and then edit it, just using 2 lines of code, 2 public Transform variables.

 public Transform primaryEquipped;
 public Transform secondaryEquipped;

Then, you'd attach the current weapons/tools the player starts out with, respectively. So primary would be something like a gun, and secondary would be something like a knife. You could also just have 2 primaries, or more.

Next, you make your weapon switching script and attach that to the player. Inside it, this is code I use:

 public int currentEquipped = 1;
 //1 would be the default weapon equipped, so maybe the axe or the pickaxe. You'll see why we use an int, later.
 
 private Transform player;
 //this is the player we're going to find, and then get the 'EquippedWeapons' script to access the current primary/secondary.
 
 
 void Start()
 {
     player = GameObject.FindObjectWithTag("Player").transform;
     //Remember to make your players tag "Player"!
 
     currentEquipped = 1;
     //Set the default weapon, just in case the int was 0.
 }    
 
 void Update()
 {
     if(Input.GetKeyDown(KeyCode.Alpha01) && currentEquipped != 1)
         currentEquipped = 1; //we ask it if it's not 1 that way we cant switch to the weapon we already have equipped
 
     if(Input.GetKeyDown(KeyCode.Alpha02) && currentEquipped != 2)
         currentEquipped = 2; //Same thing
 
     switch(currentEquipped) //Learn more about switch statements on the docs
     {    
         case 1:
             player.GetComponent<EquippedWeapons>().currentPrimary.gameObect.SetActive(true);
             player.GetComponent<EquippedWeapons>().currentSecondary.gameObect.SetActive(false);
             //we set the primary active if it's 1, and disable the secondary if it's 1.
             break;
         case 2:
             player.GetComponent<EquippedWeapons>().currentPrimary.gameObect.SetActive(false);
             player.GetComponent<EquippedWeapons>().currentSecondary.gameObect.SetActive(true);
             //same thing
             break;
         //case 3, case 4, case 5...etc. you can expand this however long you want.
         
         default:
             player.GetComponent<EquippedWeapons>().currentPrimary.gameObect.SetActive(true);
             player.GetComponent<EquippedWeapons>().currentSecondary.gameObect.SetActive(false);
             break;
 
             //we set the default just incase the int is 0.
     }
 
 }


If you got any questions just ask! Hope I helped!

Comment
Add comment · Show 3 · 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 ZachAttack9280 · Oct 20, 2015 at 11:49 AM 0
Share

so do i need to make two scripts? if so, here are the two,

WeaponsSwitch public int currentEquipped = 1; //1 would be the default weapon equipped, so maybe the axe or the pickaxe. You'll see why we use an int, later.

 private Transform player;
 //this is the player we're going to find, and then get the 'EquippedWeapons' script to access the current primary/secondary.
 
 
 void Start()
 {
     player = GameObject.FindObjectWithTag("Player").transform;
     //Remember to make your players tag "Player"!
     
     currentEquipped = 1;
     //Set the default weapon, just in case the int was 0.
 }    
 
 void Update()
 {
     if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Alpha01) && currentEquipped != 1)
         currentEquipped = 1; //we ask it if it's not 1 that way we cant switch to the weapon we already have equipped
     
     if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Alpha02) && currentEquipped != 2)
         currentEquipped = 2; //Same thing
     
     switch(currentEquipped) //Learn more about switch statements on the docs
     {    
     case 1:
         player.GetComponent<EquippedWeapons>().currentPrimary.gameObect.SetActive(true);
         player.GetComponent<EquippedWeapons>().currentSecondary.gameObect.SetActive(false);
         //we set the primary active if it's 1, and disable the secondary if it's 1.
         break;
     case 2:
         player.GetComponent<EquippedWeapons>().currentPrimary.gameObect.SetActive(false);
         player.GetComponent<EquippedWeapons>().currentSecondary.gameObect.SetActive(true);
         //same thing
         break;
         //case 3, case 4, case 5...etc. you can expand this however long you want.
         
     default:
         player.GetComponent<EquippedWeapons>().currentPrimary.gameObect.SetActive(true);
         player.GetComponent<EquippedWeapons>().currentSecondary.gameObect.SetActive(false);
         break;
         
         //we set the default just incase the int is 0.
     }
 }
 

EquippedWeapons

 public Transform primaryEquipped;
 public Transform secondaryEquipped;

But then i get tons of errors:

Assets/EquippedWeapons.cs(1,18): error CS0116: A namespace can only contain types and namespace declarations Assets/EquippedWeapons.cs(2,18): error CS0116: A namespace can only contain types and namespace declarations Assets/WeaponsSwith.cs(1,12): error CS0116: A namespace can only contain types and namespace declarations Assets/WeaponsSwith.cs(4,19): error CS0116: A namespace can only contain types and namespace declarations Assets/WeaponsSwith.cs(8,6): error CS0116: A namespace can only contain types and namespace declarations Assets/WeaponsSwith.cs(17,6): error CS0116: A namespace can only contain types and namespace declarations

avatar image ZachAttack9280 ZachAttack9280 · Oct 19, 2015 at 08:52 PM 0
Share

never$$anonymous$$d, fixed that but now i get this error:

Assets/WeaponsSwitch.cs(4,14): error CS0101: The namespace global::' already contains a definition for WeaponsSwitch'

here is the code:

 using UnityEngine;
 using System.Collection;
 
 public class WeaponsSwitch : $$anonymous$$onoBehaviour {
 
 public int currentEquipped = 1;
 //1 would be the default weapon equipped, so maybe the axe or the pickaxe. You'll see why we use an int, later.
 
 private Transform player;
 //this is the player we're going to find, and then get the 'EquippedWeapons' script to access the current primary/secondary.
 
 
 void Start()
 {
     player = GameObject.FindObjectWithTag("Player").transform;
     //Remember to make your players tag "Player"!
     
     currentEquipped = 1;
     //Set the default weapon, just in case the int was 0.
 }    
 
 void Update()
 {
     if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Alpha01) && currentEquipped != 1)
         currentEquipped = 1; //we ask it if it's not 1 that way we cant switch to the weapon we already have equipped
     
     if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Alpha02) && currentEquipped != 2)
         currentEquipped = 2; //Same thing
     
     switch(currentEquipped) //Learn more about switch statements on the docs
     {    
     case 1:
         player.GetComponent<EquippedWeapons>().currentPrimary.gameObect.SetActive(true);
         player.GetComponent<EquippedWeapons>().currentSecondary.gameObect.SetActive(false);
         //we set the primary active if it's 1, and disable the secondary if it's 1.
         break;
     case 2:
         player.GetComponent<EquippedWeapons>().currentPrimary.gameObect.SetActive(false);
         player.GetComponent<EquippedWeapons>().currentSecondary.gameObect.SetActive(true);
         //same thing
         break;
         //case 3, case 4, case 5...etc. you can expand this however long you want.
         
     default:
         player.GetComponent<EquippedWeapons>().currentPrimary.gameObect.SetActive(true);
         player.GetComponent<EquippedWeapons>().currentSecondary.gameObect.SetActive(false);
         break;
         
         //we set the default just incase the int is 0.
           }
         }
 }
avatar image Whiteleaf · Oct 20, 2015 at 03:09 PM 0
Share

You must have another script named "WeaponsSwitch", which is why it's telling you that.

Trying na$$anonymous$$g it something different both in the editor and in the script, that should fix it. just make sure the name in the editor and in the script are the same or it'll throw up more errorrs.

avatar image
0

Answer by Addyarb · Oct 19, 2015 at 06:37 AM

Both line 15 and line 20 are seeking the same object. Change one of them to "pickaxe."

Here's a C# script that may make your life easier (just convert to JS if desired). Your current solution uses string references which you will find most people discourage at most capacities. Try to only use string references in start functions, and even then using [SerializeField] on a private variable or using public variables is a cleaner and more reliable way. Just remember to back up your work!

 public GameObject axe, pickaxe;
 
 public void SwapWeapons(){
 if(axe.activeInHierarchy){
  axe.SetActive(false);
  pickaxe.SetActive(true);
 else{
 if(pickaxe.activeInHierarchy){
  axe.SetActive(true);
  pickaxe.SetActive(false);
 }

}

Comment
Add comment · Show 5 · 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 ZachAttack9280 · Oct 19, 2015 at 07:22 AM 0
Share

@Addyarb i get errors

Assets/WeaponsSwitch.cs(13,28): error CS1525: Unexpected symbol `else'

Assets/WeaponsSwitch.cs(17,26): error CS8025: Parsing error

avatar image ZachAttack9280 ZachAttack9280 · Oct 19, 2015 at 07:23 AM 0
Share

and here is the script

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class WeaponsSwitch : $$anonymous$$onoBehaviour {
 
 public GameObject axe, pickaxe;
 
 public void SwapWeapons(){
     if(axe.activeInHierarchy){
         axe.SetActive(false);
         pickaxe.SetActive(true)
             else
             if(pickaxe.activeInHierarchy){
                 axe.SetActive(true);
                 pickaxe.SetActive(false);
             }
         }
     }
 }
avatar image Addyarb ZachAttack9280 · Oct 19, 2015 at 07:44 AM 0
Share

Updated my code, just forgot to put in a couple brackets. Typed it in on notepad ins$$anonymous$$d of a complier!

Show more comments

Follow this Question

Answers Answers and Comments

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

Related Questions

Weapon Switching 2 Answers

Weapon switching. 1 Answer

Deactivate children with .active = false 2 Answers

Controlling Weapons With MouseWheel 2 Answers

weapon reload malfuction 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