The active inventory issue featured and fixed in this section of the 2D rpg combat course was not fixed for me and I am unsure why.
active inventory script below:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ActiveInventory : MonoBehaviour
{
private int activeSlotIndexNum = 0;
private PlayerControls playerControls;
private void Awake(){
playerControls = new PlayerControls();
}
private void Start() {
playerControls.Inventory.Keyboard.performed += ctx => ToggleActiveSlot((int)ctx.ReadValue<float>());
ToggleActiveHighlight(0);
}
private void OnEnable() {
playerControls.Enable();
}
private void ToggleActiveSlot(int numValue){
ToggleActiveHighlight(numValue-1);
}
private void ToggleActiveHighlight(int indexNum){
activeSlotIndexNum = indexNum;
foreach (Transform inventorySlot in this.transform) {
inventorySlot.GetChild(0).gameObject.SetActive(false);
}
this.transform.GetChild(indexNum).GetChild(0).gameObject.SetActive(true);
this.transform.GetChild(indexNum).GetChild(0).gameObject.SetActive(true);
ChangeActiveWeapon();
}
private void ChangeActiveWeapon (){
if (ActiveWeapon.Instance.CurrentActiveWeapon != null){
Destroy(ActiveWeapon.Instance.CurrentActiveWeapon.gameObject);
}
Transform childTransform = transform.GetChild(activeSlotIndexNum);
InventorySlot inventorySlot = childTransform.GetComponent<InventorySlot>();
WeaponInfo weaponInfo = inventorySlot.GetWeaponInfo();
GameObject weaponToSpawn = weaponInfo.weaponPrefab;
if (weaponInfo==null){
ActiveWeapon.Instance.WeaponNull();
return;
}
GameObject newWeapon = Instantiate(weaponToSpawn, ActiveWeapon.Instance.transform.position, Quaternion.identity);
ActiveWeapon.Instance.transform.rotation = Quaternion.Euler(0,0,0);
newWeapon.transform.parent = ActiveWeapon.Instance.transform;
ActiveWeapon.Instance.NewWeapon(newWeapon.GetComponent<MonoBehaviour>());
}
}