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;
}
}
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!
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);
}
}