Forum begins after the advertisement:


[Part 15]I can’t create Knife

Viewing 15 posts - 1 through 15 (of 16 total)
  • Author
    Posts
  • #13255
    yosnac2 yosnac2
    Level 3
    Participant
    Helpful?
    Up
    0
    ::

    When I start the game by following the content in the latest videos(Part 15), no knife appears and no attack occurs. I’m not getting any errors, everything looks fine but the knife is not created.

    İmgs

    #13260
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    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.

    #13263
    yosnac2 yosnac2
    Level 3
    Participant
    Helpful?
    Up
    0
    ::

    Thanks for helping but. I checked but it doesn’t even show up in inventory. I took the second screenshot after the game started.

    View post on imgur.com

    (i did choose character.)

    #13264
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    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();
        }
    #13265
    yosnac2 yosnac2
    Level 3
    Participant
    Helpful?
    Up
    0
    ::

    i did

    View post on imgur.com

    it’s working. But same

    #13266
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    Can you show me a screenshot of the console after you start the game?

    #13270
    yosnac2 yosnac2
    Level 3
    Participant
    Helpful?
    Up
    0
    ::

    View post on imgur.com

    i dont have any error

    #13272
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    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.

    #13276
    yosnac2 yosnac2
    Level 3
    Participant
    Helpful?
    Up
    0
    ::

    its right i did chose Whip.

    #13278
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    Is your whip working?

    #13283
    yosnac2 yosnac2
    Level 3
    Participant
    Helpful?
    Up
    0
    ::

    No :(

    #13293
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    Can you share your PlayerInventory script here?

    #13309
    yosnac2 yosnac2
    Level 3
    Participant
    Helpful?
    Up
    0
    ::
    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);
        }
    
    }
    

    View post on imgur.com

    #13315
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    Add the following lines to your Add() function in PlayerInventory, then show me what appears on the Console when you start the game. I’m trying to check which part of your Add() 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;
        }
    #13323
    yosnac2 yosnac2
    Level 3
    Participant
    Helpful?
    Up
    0
    ::
    
     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;
     }

    `

    View post on imgur.com

    last two not working

Viewing 15 posts - 1 through 15 (of 16 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: