I’m trying to create a script where the player is able to cycle between their two weapons by pressing a button. At the moment I have it set to P, but that’s just a random key I decided on.
using UnityEngine;
using System.Collections;
public class WeaponSwitching : MonoBehaviour
{
public enum gunCycle { Pistol, MachineGun }
public gunCycle GunCycle = gunCycle.Pistol;
private bool pistol, machineGun;
// Use this for initialization
void Start()
{
pistol = true;
machineGun = false;
}
void WeaponCycle()
{
if (Input.GetKeyDown(KeyCode.P))
{
if (pistol == true && machineGun == false)
{
machineGun = true;
pistol = false;
}
else if (pistol == false && machineGun == true)
{
pistol = true;
machineGun = false;
}
}
if (pistol == false)
{
if (GunCycle == gunCycle.Pistol)
{
gameObject.SetActive(false);
}
else if (pistol == true)
{
if (GunCycle == gunCycle.Pistol)
{
gameObject.SetActive(true);
}
}
}
if (machineGun == false)
{
if (GunCycle == gunCycle.MachineGun)
{
gameObject.SetActive(false);
}
else if (machineGun == true)
{
if (GunCycle == gunCycle.MachineGun)
{
gameObject.SetActive(true);
}
}
}
}
// Update is called once per frame
void Update()
{
WeaponCycle();
}
}
I’ve noticed, in the inspector, the machine gun will start off deactivated, which I want, but when I press P, not only does pistol deactivate, also wanted, but the machine gun doesn’t reactivate, which is what I don’t want. Any advice on what I’ve done wrong?
When you deactivate a gameObject any components on it no longer run. Since your deactivation script lives on the object you’re deactivating it will no longer run so won’t see that you hit the toggle key again. The solution is to move the toggle functionality into a different script that lives on a game object that doesn’t ever get deactivated.
EDIT: Recycle your scripts on the weapons to some nice ascii art and add this one instead to your singleton game object:
public class WeaponSwitching : MonoBehaviour
{
public GameObject[] weapons; // Assign the weapons here in the inspector
public int initialWeaponIndex; // Set the initial weapon index in the inspector
private selectedWeaponIndex;
// Use this for initialization
void Start()
{
// Your advertisement here!
selectedWeaponIndex = initialWeaponIndex;
ApplyWeaponStatus();
}
private void WeaponCycle()
{
if (Input.GetKeyDown(KeyCode.P))
{
selectedWeaponIndex = (selectedWeaponIndex + 1) % weapons.Length;
ApplyWeaponState();
}
}
// Update is called once per frame
void Update()
{
WeaponCycle();
}
private void ApplyWeaponStatus() {
for (int i=0; i<weapons.Length; i++)
weapons*.SetActive(i == selectedWeaponIndex);*
Thanks for the help so far, everyone. I haven’t been able to get it to work yet, but I have made some progress so far. I now have a new issue, and a weird one at that. When the game first starts up, the pistol is set to true, and machine gun is set to false. When I press P, both weapons are set to true and false at the same time. I didn’t even know this was possible. I figured an error would come up to stop that, or something. I’ve set a third variable in the enum called AlwaysActive. I’ve also created an empty game object and attached the script to this object. So now I’m able to freely switch between the states of the weapons, but now, which ever weapon I had active at the start of the game, stays active regardless of it being true, false, or both at the same time.
using UnityEngine;
using System.Collections;
public class WeaponSwitching : MonoBehaviour
{
public enum gunCycle { Pistol, MachineGun, AlwaysActive }
public gunCycle GunCycle = gunCycle.AlwaysActive;
private bool pistol, machineGun,AlwaysActive;
// Use this for initialization
void Start()
{
AlwaysActive = true;
pistol = false;
machineGun = true;
}
void WeaponCycle()
{
if (AlwaysActive == true && GunCycle == gunCycle.AlwaysActive)
{
if (Input.GetKeyDown(KeyCode.P))
{
if (pistol == true && machineGun == false)
{
machineGun = true;
pistol = false;
}
else if (pistol == false && machineGun == true)
{
pistol = true;
machineGun = false;
}
}
}
if (!pistol && GunCycle == gunCycle.Pistol)
{
gameObject.SetActive(false);
}
else if (pistol & GunCycle == gunCycle.Pistol)
{
gameObject.SetActive(true);
}
if (!machineGun && GunCycle == gunCycle.MachineGun)
{
gameObject.SetActive(false);
}
else if (machineGun && GunCycle == gunCycle.MachineGun)
{
gameObject.SetActive(true);
}
}
// Update is called once per frame
void Update()
{
WeaponCycle();
}
}