Search Unity

Best way to deal with my Virtual use Method for using Consumables?

Discussion in 'Scripting' started by SaamBell, Jun 25, 2019.

  1. SaamBell

    SaamBell

    Joined:
    Mar 28, 2014
    Posts:
    128
    Been following Brackeys rpg Tutorial and changed it into something quite different! Instead of an RPG i'm making a Castaway Sims Collection/Gathering/Crafting type game and over the last few days I've highly enjoyed the ride! I've reached a point now however where i'm stuck, and I will try and explain best I can the problem I'm facing.

    upload_2019-6-25_14-52-49.png

    As you can see from the photo above, I have two of the fruits in my current Inventory. The Orange comes for the class of Food, which pulls from the base Scriptable Object of item. What I'm not trying to do is get the virtual use method function from my item, override it inside the Food class and then add however much value to the sliders (top corners of the screen). So far this is my code

    StatsScript Method
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class StatScripts : MonoBehaviour
    7. {
    8.     public Slider hungerSlider;
    9.     public Slider restSlider;
    10.     public Slider socialSlider;
    11.     public Slider hygineSlider;
    12.     public Slider bathroomSlider;
    13.     public Slider comfortSlider;
    14.  
    15.  
    16. }
    17.  
    Item Script

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. [CreateAssetMenu(fileName ="New Item", menuName = "Inventory/Item")]
    4. public class Item : ScriptableObject {
    5.  
    6.  
    7.     public StatScripts stats;
    8.     new public string name = "New Item";
    9.     public Sprite icon = null;
    10.     public bool isKeyItem = false;
    11.     public int hungerAmount;
    12.  
    13.  
    14. public virtual void Use()
    15. {
    16.  
    17.         stats = GameObject.FindGameObjectWithTag("Character").GetComponent<StatScripts>();
    18.  
    19.         Debug.Log("Using " + name);
    20.  
    21.  
    22.     }
    23.     public void RemoveItemFromInventory()
    24.     {
    25.         Inventory.instance.Remove(this);
    26.     }
    27. }
    Food Script

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [CreateAssetMenu(fileName = "New Item", menuName = "Inventory/Food")]
    6. public class Food : Item
    7. {
    8.  
    9.  
    10.     public override void Use()
    11.     {
    12.         Debug.Log("Overwritten Food Taking Over!");
    13.         base.Use();
    14.         stats.hungerSlider.value = stats.hungerSlider.value += hungerAmount;
    15.         RemoveItemFromInventory();
    16.     }
    17.  
    18.  
    19.  
    20. }

    InventorySlot Script


    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using TMPro;
    6.  
    7. public class InventorySlot : MonoBehaviour
    8. {
    9.  
    10.     public TextMeshProUGUI itemText;
    11.     public Image icon;
    12.     Item item;
    13.     public Button removeButton;
    14.     public GameObject dropItemModal;
    15.     public Animator spriteAnim;
    16.     public Button yesButton;
    17.  
    18.     public void Start()
    19.     {
    20.      
    21.  
    22.     }
    23.  
    24.     public void AddItem (Item newItem)
    25.     {
    26.      
    27.      
    28.         spriteAnim.Play("SpriteBounce");
    29.         item = newItem;
    30.         icon.sprite = item.icon;
    31.         icon.enabled = true;
    32.         removeButton.interactable = true;
    33.    
    34.    
    35.  
    36.     }
    37.  
    38.     public void ClearSlot()
    39.     {
    40.         item = null;
    41.         icon.sprite = null;
    42.         icon.enabled = false;
    43.         removeButton.interactable = false;
    44.     }
    45.  
    46.     public void onRemoveButton()
    47.     {
    48.  
    49.          Inventory.instance.Remove(item);
    50.  
    51.         #region
    52.         /*   dropItemModal.SetActive(true);
    53.         yesButton.onClick.AddListener(onYesClick);
    54.            itemText.text = item.name;
    55.  
    56.                   if (item.isKeyItem != false)
    57.                   {
    58.                       yesButtonDisabled.interactable = false;
    59.                       itemText.text = "You Can't Drop a Key Item!";
    60.                   }
    61.                   else
    62.                   {
    63.                       yesButtonDisabled.interactable = true;
    64.                   }
    65.                   */
    66.         #endregion
    67.  
    68.     }
    69.     #region
    70.     /* public void onYesClick()
    71.      {
    72.        //  Inventory.instance.Remove(item);
    73.          dropItemModal.SetActive(false);
    74.      }
    75.  
    76.      public void onNoClick()
    77.      {
    78.          dropItemModal.SetActive(false);
    79.      } */
    80.     #endregion
    81.  
    82.     public void UseItem()
    83.     {
    84.  
    85.         if (item != null)
    86.         {
    87.          
    88.             item.Use();
    89.         }
    90.     }
    91. }
    92.