I'm trying to switch between weapons with (1, 2) but I need to destroy the weapon that I'm carrying when one of the two buttons had been pressed Code (CSharp): void Start() { managerWeapon = GameObject.Find("ManagerWeapon").GetComponent<ManagerWeapon>(); player = GameObject.Find("Player").GetComponent<PlayerController>(); primaryWeapon = managerWeapon.weapons[0]; Instantiate(primaryWeapon, pivotRifle); } void Update() { ChangeWeapon(indexPerviousWeapon); } public void ChangeWeapon(int weaponIndex) { indexPerviousWeapon = weaponIndex; primaryWeapon = managerWeapon.weapons[0]; secondaryWeapon = managerWeapon.weapons[1]; if(Input.GetKeyDown(KeyCode.Alpha1)) { Instantiate(primaryWeapon, pivotRifle); player.playerAnimator.SetLayerWeight(0, 1f); } if(Input.GetKeyDown(KeyCode.Alpha2)) { Instantiate(secondaryWeapon, pivotGun); player.playerAnimator.SetLayerWeight(1, 1f); } } }
You'll want to cache a reference to the current weapon instance so you can destroy it when changing to a new one. You'll probably also want to cache the index of the weapon so you can avoid doing anything if the weapon in question is already equipped. Code (CSharp): int currentWeaponIndex = -1; Weapon currentWeapon = null; void Start() { managerWeapon = GameObject.Find("ManagerWeapon").GetComponent<ManagerWeapon>(); player = GameObject.Find("Player").GetComponent<PlayerController>(); SetCurrentWeapon(0); } void Update() { if(Input.GetKeyDown(KeyCode.Alpha1)) { SetCurrentWeapon(0); } else if(Input.GetKeyDown(KeyCode.Alpha2)) { SetCurrentWeapon(1); } } void SetCurrentWeapon(int weaponIndex) { if(currentWeaponIndex == weaponIndex) { return; } if(currentWeapon != null) { Destroy(currentWeapon.gameObject); } currentWeaponIndex = index; var prefab = GetWeaponPrefab(weaponIndex); var parent = GetWeaponParent(weaponIndex); currentWeapon = Instantiate(prefab, parent); int layerIndex = GetAnimatorLayerIndex(weaponIndex); player.playerAnimator.SetLayerWeight(layerIndex, 1f); } Weapon GetWeaponPrefab(int weaponIndex) => managerWeapon.weapons[weaponIndex]; Transform GetWeaponParent(int weaponIndex) => weaponIndex == 0 ? pivotRifle : pivotGun; int GetAnimatorLayerIndex(int weaponIndex) => weaponIndex;
Thanks for your reply and your time, First of all I'm still learning and trying to understand the logic behind the code, I understand some of what you told me but I'm still confused from line 36 to the end.
Ah apologies, I was using some a bit more advanced C# features in my code just to compress everything into a smaller space without thinking about it The three members at the bottom are just normal methods but using the => syntax so they can be written in a single line. So this: Code (CSharp): int GetAnimatorLayerIndex(int weaponIndex) => weaponIndex; Is exactly the same as this: Code (CSharp): int GetAnimatorLayerIndex(int weaponIndex) { return weaponIndex; } Inside the GetWeaponParent method I was also using the ?: operator which is just shorthand for if-else. So this: Code (CSharp): Transform GetWeaponParent(int weaponIndex) => weaponIndex == 0 ? pivotRifle : pivotGun; Is exactly the same as this: Code (CSharp): Transform GetWeaponParent(int weaponIndex) { if(weaponIndex == 0) { return pivotRifle; } else { return pivotGun; } }
Thanks a lot for the explanation it helped me a lot, and I think I've got an issue in the manager weapon script as I created the array but didn't sort the weapons in it.. does that make any problem with the manager weapon change script the Manager Weapon script is only an empty game object and attached the manager weapon script and created an game object array only..
Good to hear that clarified things! I don't quite understand what you mean about not having sorted the weapons in the array?
in the weapon script, I only created the game objects array without any other things Code (CSharp): public class ManagerWeapon : MonoBehaviour { public GameObject[] weapons; void Start() { }
Yeah you can just use a plain GameObject array instead of a Weapon array just as well. Just change Weapon currentWeapon into GameObject currentWeapon and Destroy(currentWeapon.gameObject) into Destroy(currentWeapon).
Thank you a lot you explained a lot to me and enlighten me with a lot of new ways on how to approach things like that I've only one question ? currentWeapon = index (what does index refer to here) Code (CSharp): currentWeaponIndex = index; var prefab = GetWeaponPrefab(weaponIndex); var parent = GetWeaponParent(weaponIndex); currentWeapon = Instantiate(prefab, parent);