Search Unity

Trouble getting the a value held by my base ScriptableObject. Inventory System. C#.

Discussion in 'Scripting' started by FullMe7alJacke7, Feb 11, 2018.

  1. FullMe7alJacke7

    FullMe7alJacke7

    Joined:
    Dec 17, 2013
    Posts:
    55
    Hey guys I have looked all over, watched tutorials, etc, and after 4 days I've decided to break down and ask for help.

    How can I read data being passed through a ScriptableObject from another SO?

    Example:
    Items > WeaponsSO or ArmorSO > MyItems > DropZone
    Base SO Script > Inherited SO Script > MonoBehavior > MonoBehavior

    The Items script just holds an enum that I am using to determine what type of item slot said item goes to.
    I put it there thinking that it would apply to both WeaponsSO and ArmorSO (Which is does) but I can't figure out how to read that specific SO's property to figure out the assigned item slot.

    This is the base ScriptableObject script.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEditor;
    5.  
    6. [System.Serializable]
    7. public class Items : ScriptableObject
    8. {
    9.     //Different Equipment Slot Types
    10.     public enum Slot
    11.     {
    12.         HELMET = 0,
    13.         NECKLACE = 1,
    14.         EARRINGS = 2,
    15.         CAPE = 3,
    16.         TORSO = 4,
    17.         GLOVES = 5,
    18.         LEFTRING = 6,
    19.         RIGHTRING = 7,
    20.         BELT = 8,
    21.         LEGGINGS = 9,
    22.         BOOTS = 10,
    23.         TRINKET = 11,
    24.         MAINHAND = 12,
    25.         OFFHAND = 13,
    26.         INVENTORY = 14
    27.     }
    28.     [SerializeField] public Slot assignedWeaponSlot;
    29.  
    30.  
    31.     public Slot WeaponSlot
    32.     {
    33.  
    34.         get { return this.assignedWeaponSlot; }
    35.         private set { this.assignedWeaponSlot = value; }
    36.     }
    37. }


    This is the MonoBehavior on my UI Panels that is supposed to be using the value from Items to determine the dropped item's slot.

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.EventSystems;
    6. using UnityEditor;
    7.  
    8. public class DropZone : MonoBehaviour, IDropHandler, IPointerEnterHandler, IPointerExitHandler {
    9.  
    10.     public int filledCapacity = 0;
    11.     [SerializeField] private int maxCapacity = 1;
    12.     public Items.Slot typeOfItemsAllowed = Items.Slot.INVENTORY;
    13.     Draggable draggable;
    14.  
    15.     public void OnDrop(PointerEventData eventData)
    16.     {
    17.         draggable = eventData.pointerDrag.GetComponent<Draggable>();
    18.         Debug.Log(eventData.pointerDrag.name);
    19.         if (draggable != null)
    20.         {
    21.             if (typeOfItemsAllowed == Items.Slot.INVENTORY)  //OR check goes here.  
    22.             {
    23.                 if (filledCapacity >= maxCapacity)
    24.                 {
    25.  
    26.                     //TODO Add in Auto-Swap Equipment Function
    27.                     Debug.LogWarning("Inventory slot is full, please remove items before trying again.");
    28.                     return;
    29.                 }
    30.                 draggable.preDragParent = this.transform;
    31.                 filledCapacity++;
    32.             } else
    33.             {
    34.                 Debug.Log(draggable.name + " could not be dropped on the slot.");
    35.             }
    36.         }
    37.     }
    38.  
    39.     public void OnPointerEnter(PointerEventData eventData)
    40.     {
    41.         if (draggable == null)
    42.         {
    43.             Debug.LogWarning("Nothing is being draggged...");
    44.         }
    45.         else if (draggable != null)
    46.         {
    47.             if (typeOfItemsAllowed == Items.Slot.INVENTORY)
    48.             {
    49.                 draggable.placeholderParent = this.transform;
    50.             }
    51.         }
    52.     }
    53.  
    54.     public void OnPointerExit(PointerEventData eventData)
    55.     {
    56.         if (draggable == null)
    57.         {
    58.             Debug.LogWarning("Nothing is being draggged...");
    59.         }
    60.         else if (draggable != null)
    61.         {
    62.             if (typeOfItemsAllowed == Items.Slot.INVENTORY)
    63.             {
    64.                 draggable.placeholderParent = draggable.preDragParent;
    65.             }
    66.         }
    67.     }
    68. }
    The posted script doesn't have it in currently but the if statement that checks for the INVENTORY slot just gets an OR added in that checks "Does the item type match my allowed items? if not am I set to inventory? If I am then IDC what the item type is anyway...

    I think whats happening is the data is being read from an instance of Items and its enum index value defaults to 1 when an instance is created, so I still need to figure out how to read from a SO attached to my MonoBehavior script... struggling to make this work for some reason...
     
    Last edited: Feb 12, 2018