Forum begins after the advertisement:
[Part 15]I can’t create Knife
Home › Forums › Video Game Tutorial Series › Creating a Rogue-like Shoot-em Up in Unity › [Part 15]I can’t create Knife
- This topic has 15 replies, 2 voices, and was last updated 9 months, 1 week ago by Terence.
-
AuthorPosts
-
February 7, 2024 at 12:55 am #13255February 8, 2024 at 12:10 am #13260::
Hi yosnac2, check to see if any projectiles appear in your Hierarchy. It may be that your Knife prefab is spawning, but is under the map tiles.
February 8, 2024 at 1:04 am #13263::Thanks for helping but. I checked but it doesn’t even show up in inventory. I took the second screenshot after the game started.
(i did choose character.)
February 8, 2024 at 1:30 am #13264::Do you have this line in your PlayerStats? If you do, can you add the green line and see if a message prints?
void Start() { //Spawn the starting weapon inventory.Add(characterData.StartingWeapon); print(characterData.StartingWeapon); //Initialize the experience cap as the first experience cap increase experienceCap = levelRanges[0].experienceCapIncrease; //Set the current stats display GameManager.instance.currentHealthDisplay.text = "Health: " + CurrentHealth; GameManager.instance.currentRecoveryDisplay.text = "Recovery: " + CurrentRecovery; GameManager.instance.currentMoveSpeedDisplay.text = "Move Speed: " + CurrentMoveSpeed; GameManager.instance.currentMightDisplay.text = "Might: " + CurrentMight; GameManager.instance.currentProjectileSpeedDisplay.text = "Projectile Speed: " + CurrentProjectileSpeed; GameManager.instance.currentMagnetDisplay.text = "Magnet: " + CurrentMagnet; GameManager.instance.AssignChosenCharacterUI(characterData); UpdateHealthBar(); UpdateExpBar(); UpdateLevelText(); }
February 8, 2024 at 1:36 am #13265February 8, 2024 at 10:25 am #13266February 9, 2024 at 3:05 am #13270February 9, 2024 at 12:40 pm #13272::In the Console, it says that the weapon assigned is Whip. Is that correct?
If not, I will need you to post your PlayerInventory script here as well. I suspect there is something missing in the
Add()
function, because your weapon GameObject is not getting created under the player.February 10, 2024 at 2:30 am #13276February 10, 2024 at 2:01 pm #13278February 10, 2024 at 8:54 pm #13283February 11, 2024 at 8:48 am #13293February 11, 2024 at 6:18 pm #13309::using System; using System.Collections.Generic; using TMPro; using UnityEngine; using UnityEngine.UI; public class PlayerInventory : MonoBehaviour { [System.Serializable] public class Slot { public Item item; public Image image; public void Assign(Item assignedItem) { item = assignedItem; if (item is Weapon) { Weapon weapon = item as Weapon; image.enabled = true; image.sprite = weapon.data.icon; } else { Passive passive = item as Passive; image.enabled = true; image.sprite = passive.data.icon; } Debug.Log(string.Format("Assigned {0} to player", item.name)); } public void Clear() { item = null; image.enabled = false; image.sprite = null; } public bool IsEmpty() { return item != null; } } public List<Slot> weaponSlots = new List<Slot>(6); public List<Slot> passiveSlots = new List<Slot>(6); [System.Serializable] public class UpgradeUI { public TMP_Text upgradeNameDisplay; public TMP_Text upgradeDescriptionDisplay; public Image upgradeIcon; public Button upgradeButton; } [Header("UI Elements")] public List<WeaponData> availableWeapons = new List<WeaponData>(); public List<PassiveData> availablePassives = new List<PassiveData>(); public List<UpgradeUI> upgradeUIOptions = new List<UpgradeUI>(); PlayerStats player; private void Start() { player = GetComponent<PlayerStats>(); } public bool Has(ItemData Type) { return Get(Type); } public Item Get(ItemData Type) { if (Type is WeaponData) { return Get(Type as WeaponData); } else if (Type is PassiveData) { return Get(Type as PassiveData); } return null; } public Weapon Get(WeaponData type) { foreach (Slot slots in weaponSlots) { Weapon weapon = slots.item as Weapon; if (weapon.data == type) { return weapon; } } return null; } public bool Remove(WeaponData data, bool removeUpgradeAvailability = false) { if (removeUpgradeAvailability) { availableWeapons.Remove(data); } for (int i = 0; i < weaponSlots.Count; i++) { Weapon weapon = weaponSlots[i].item as Weapon; if (weapon.data == data) { weaponSlots[i].Clear(); weapon.OnUnequip(); Destroy(weapon.gameObject); return true; } } return false; } public bool Remove(PassiveData data, bool removeUpgradeAvailability = false) { if (removeUpgradeAvailability) { availablePassives.Remove(data); } for (int i = 0; i < weaponSlots.Count; i++) { Passive passive = weaponSlots[i].item as Passive; if (passive.data == data) { weaponSlots[i].Clear(); passive.OnUnequip(); Destroy(passive.gameObject); return true; } } return false; } public bool Remove(ItemData data, bool removeUpgradeAvailability = false) { if (data is PassiveData) { return Remove(data as PassiveData, removeUpgradeAvailability); } else if (data is WeaponData) { return Remove(data as WeaponData, removeUpgradeAvailability); } return false; } public int Add(WeaponData data) { int slotNum = -1; for (int i = 0; i < weaponSlots.Capacity; i++) { if (weaponSlots[i].IsEmpty()) { slotNum = i; break; } } if (slotNum < 0) { return slotNum; } Type weaponType = Type.GetType(data.behaviour); if (weaponType != null) { GameObject go = new GameObject(data.baseStats.name + "Controller"); Weapon spawnedWeapon = (Weapon)go.AddComponent(weaponType); spawnedWeapon.Initialise(data); spawnedWeapon.transform.SetParent(transform); spawnedWeapon.transform.localPosition = Vector2.zero; spawnedWeapon.OnEquip(); weaponSlots[slotNum].Assign(spawnedWeapon); if (GameManager.instance != null && GameManager.instance.choosingUpgrade) { GameManager.instance.EndLevelUp(); } return slotNum; } else { Debug.LogWarning(string.Format("Invalid weapon type specified for{0}.", data.name)); } return -1; } public int Add(PassiveData data) { int slotNum = -1; for (int i = 0; i < passiveSlots.Capacity; i++) { if (passiveSlots[i].IsEmpty()) { slotNum = i; break; } } if (slotNum < 0) { return slotNum; } GameObject go = new GameObject(data.baseStats.name + "Passive"); Passive passive = go.AddComponent<Passive>(); passive.Initialise(data); passive.transform.SetParent(transform); passive.transform.localPosition = Vector2.zero; passiveSlots[slotNum].Assign(passive); if (GameManager.instance != null && GameManager.instance.choosingUpgrade) { GameManager.instance.EndLevelUp(); } player.RecalculateStats(); return slotNum; } public int Add(ItemData data) { if (data is WeaponData) { return Add(data as WeaponData); } else if (data is PassiveData) { return Add(data as PassiveData); } return -1; } public void LevelUpWeapon(int slotIndex, int upgradeIndex) { if (weaponSlots.Count > slotIndex) { Weapon weapon = weaponSlots[slotIndex].item as Weapon; if (!weapon.DoLevelUp()) { Debug.LogWarning(string.Format("Failed to level up{0}", weapon.name)); return; } } if (GameManager.instance != null && GameManager.instance.choosingUpgrade) { GameManager.instance.EndLevelUp(); } } public void LevelUpPassive(int slotIndex, int upgradeIndex) { if (passiveSlots.Count > slotIndex) { Passive passive = passiveSlots[slotIndex].item as Passive; if (!passive.DoLevelUp()) { Debug.LogWarning(string.Format("Failed to level up{0}", passive.name)); return; } } if (GameManager.instance != null && GameManager.instance.choosingUpgrade) { GameManager.instance.EndLevelUp(); } player.RecalculateStats(); } private void ApplyUpgradeOptions() { List<WeaponData> availableWeaponUpgrades = new List<WeaponData>(availableWeapons); List<PassiveData> availablePassiveItemUpgrades = new List<PassiveData>(availablePassives); foreach(UpgradeUI upgradeOptions in upgradeUIOptions) { if (availableWeaponUpgrades.Count == 0 && availablePassiveItemUpgrades.Count == 0) { return; } int upgradeType; if (availableWeaponUpgrades.Count == 0) { upgradeType = 2; } else if (availablePassiveItemUpgrades.Count == 0) { upgradeType = 1; } else { upgradeType = UnityEngine.Random.Range(1, 3); } if (upgradeType == 1) { WeaponData chosenWeaponUpgrade = availableWeaponUpgrades[UnityEngine.Random.Range(0, availableWeaponUpgrades.Count)]; availableWeaponUpgrades.Remove(chosenWeaponUpgrade); if (chosenWeaponUpgrade != null) { EnableUpgradeUI(upgradeOptions); bool isLevelUp = false; for (int i = 0; i < weaponSlots.Count; i++) { Weapon weapon = weaponSlots[i].item as Weapon; if (weapon != null && weapon.data == chosenWeaponUpgrade) { if (chosenWeaponUpgrade.maxLevel <= weapon.currentLevel) { DisableUpgradeUI(upgradeOptions); isLevelUp = true; break; } upgradeOptions.upgradeButton.onClick.AddListener(() => LevelUpWeapon(i, i)); Weapon.Stats nextLevel = chosenWeaponUpgrade.GetLevelData(weapon.currentLevel + 1); upgradeOptions.upgradeDescriptionDisplay.text = nextLevel.description; upgradeOptions.upgradeNameDisplay.text = nextLevel.name; upgradeOptions.upgradeIcon.sprite = chosenWeaponUpgrade.icon; isLevelUp = true; break; } } if (!isLevelUp) { upgradeOptions.upgradeButton.onClick.AddListener(() => Add(chosenWeaponUpgrade)); upgradeOptions.upgradeDescriptionDisplay.text = chosenWeaponUpgrade.baseStats.description; upgradeOptions.upgradeNameDisplay.text = chosenWeaponUpgrade.baseStats.name; upgradeOptions.upgradeIcon.sprite = chosenWeaponUpgrade.icon; } } } else if (upgradeType == 2) { PassiveData chosenPassiveUpgrade = availablePassiveItemUpgrades[UnityEngine.Random.Range(0, availablePassiveItemUpgrades.Count)]; availablePassiveItemUpgrades.Remove(chosenPassiveUpgrade); if (chosenPassiveUpgrade != null) { EnableUpgradeUI(upgradeOptions); bool isLevelUp = false; for(int i = 0;i < passiveSlots.Count;i++) { Passive passive = passiveSlots[i].item as Passive; if (passive != null && passive.data == chosenPassiveUpgrade) { if (chosenPassiveUpgrade.maxLevel <= passive.currentLevel) { DisableUpgradeUI(upgradeOptions); isLevelUp = true; break; } upgradeOptions.upgradeButton.onClick.AddListener(() => LevelUpPassive(i, i)); Passive.Modifier nextLevel = chosenPassiveUpgrade.GetLevelData(passive.currentLevel + 1); upgradeOptions.upgradeDescriptionDisplay.text = nextLevel.description; upgradeOptions.upgradeNameDisplay.text = nextLevel.name; upgradeOptions.upgradeIcon.sprite = chosenPassiveUpgrade.icon; isLevelUp = true; break; } } if (!isLevelUp) { upgradeOptions.upgradeButton.onClick.AddListener(() => Add(chosenPassiveUpgrade)); Passive.Modifier nextLevel = chosenPassiveUpgrade.baseStats; upgradeOptions.upgradeDescriptionDisplay.text = nextLevel.description; upgradeOptions.upgradeNameDisplay.text = nextLevel.name; upgradeOptions.upgradeIcon.sprite = chosenPassiveUpgrade.icon; } } } } } private void RemoveUpgradeOptions() { foreach(UpgradeUI upgradeOption in upgradeUIOptions) { upgradeOption.upgradeButton.onClick.RemoveAllListeners(); DisableUpgradeUI(upgradeOption); } } public void RemoveAndApplyUpgrades() { RemoveUpgradeOptions(); ApplyUpgradeOptions(); } private void DisableUpgradeUI(UpgradeUI ui) { ui.upgradeNameDisplay.transform.parent.gameObject.SetActive(false); } private void EnableUpgradeUI(UpgradeUI ui) { ui.upgradeNameDisplay.transform.parent.gameObject.SetActive(true); } }
February 12, 2024 at 12:55 pm #13315::Add the following lines to your
Add()
function inPlayerInventory
, then show me what appears on the Console when you start the game. I’m trying to check which part of yourAdd()
function is failing to run.public int Add(WeaponData data) { int slotNum = -1; print("PlayerInventory.Add() called."); for (int i = 0; i < weaponSlots.Capacity; i++) { if (weaponSlots[i].IsEmpty()) { slotNum = i; break; } } print("PlayerInventory.Add(): Checking slot number - " + slotNum); if (slotNum < 0) { return slotNum; } Type weaponType = Type.GetType(data.behaviour); print("PlayerInventory.Add(): " + weaponType); if (weaponType != null) { GameObject go = new GameObject(data.baseStats.name + "Controller"); print("PlayerInventory.Add(): Controller created."); Weapon spawnedWeapon = (Weapon)go.AddComponent(weaponType); spawnedWeapon.Initialise(data); spawnedWeapon.transform.SetParent(transform); spawnedWeapon.transform.localPosition = Vector2.zero; spawnedWeapon.OnEquip(); weaponSlots[slotNum].Assign(spawnedWeapon); if (GameManager.instance != null && GameManager.instance.choosingUpgrade) { GameManager.instance.EndLevelUp(); } return slotNum; } else { Debug.LogWarning(string.Format("Invalid weapon type specified for{0}.", data.name)); } return -1; }
February 12, 2024 at 10:26 pm #13323::public int Add(WeaponData data) { int slotNum = -1; print("PlayerInventory.Add() called."); for (int i = 0; i < weaponSlots.Capacity; i++) { if (weaponSlots[i].IsEmpty()) { slotNum = i; break; } } print("PlayerInventory.Add(): Checking slot number - " + slotNum); if (slotNum < 0) { return slotNum; } Type weaponType = Type.GetType(data.behaviour); print("PlayerInventory.Add(): " + weaponType); //Not Working if (weaponType != null) { GameObject go = new GameObject(data.baseStats.name + "Controller"); print("PlayerInventory.Add(): Controller created."); //Not Working Weapon spawnedWeapon = (Weapon)go.AddComponent(weaponType); spawnedWeapon.Initialise(data); spawnedWeapon.transform.SetParent(transform); spawnedWeapon.transform.localPosition = Vector2.zero; spawnedWeapon.OnEquip(); weaponSlots[slotNum].Assign(spawnedWeapon); if (GameManager.instance != null && GameManager.instance.choosingUpgrade) { GameManager.instance.EndLevelUp(); } return slotNum; } else { Debug.LogWarning(string.Format("Invalid weapon type specified for{0}.", data.name)); } return -1; }
`
last two not working
-
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: