- Home /
The question is answered, right answer was accepted.
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;
}
}
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!
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
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.
}
}
}
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.
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);
}
}
@Addyarb i get errors
Assets/WeaponsSwitch.cs(13,28): error CS1525: Unexpected symbol `else'
Assets/WeaponsSwitch.cs(17,26): error CS8025: Parsing error
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);
}
}
}
}
Updated my code, just forgot to put in a couple brackets. Typed it in on notepad ins$$anonymous$$d of a complier!
Follow this Question
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