Search Unity

[SOLVED] Instantiate GameObject from a ScriptableObject

Discussion in 'Scripting' started by egartnuc, Jul 28, 2018.

  1. egartnuc

    egartnuc

    Joined:
    Jun 2, 2018
    Posts:
    23
    Hello reader,

    Apologees in advance if this is a borderline retarded question to bring up. Been a while since I wrote anything, usually I find answers from other threads, but been banging my head on this one. I'm gonna try to be brief.

    I'm trying to make an inventory system, with items, with a hotbar. For the items I use a scriptable object which I put a bunch of information into, as well as a GameObject. So basically on every scriptableobject I put a GameObject prefab into that field in the inspector.

    On an OnDrop PointerEventData operation (that is when I in inventory view drop the item on a hotbar slot), I'd like to instantiate this gameobject to my scene, so that it exists in the world and I can toggle it with numkeys to activate the weapon. Also, this specific item that I need instantiated is only referred to through a method.

    Below code is what I believe is relevant for the question, I cut out a lot from the full scripts to make it easier, I dare say nothing relevant is left out.

    I would be grateful for any help or suggestions. I really am trying to find answers on my own before I ask, and usually it works out. But now I'm stuck for 2 days. Appreciate it.

    BR
    Tomas

    Slot script:
    Code (CSharp):
    1.    
    2. public void AddItem(Item itemToAdd, int amnt)
    3.     {
    4.         if (itemToAdd == myItem)
    5.         {
    6.             myAmount += amnt;
    7.         }
    8.         else
    9.         {
    10.             myItem = itemToAdd;
    11.             myAmount = amnt;
    12.         }
    13.         ShowUI();
    14.     }
    15.  
    16. public void OnDrop(PointerEventData eventData)
    17.     {
    18.         // Adds item to the slot in the inventory view
    19.         AddItem(inventory.draggingItem, inventory.draggingAmount);
    20.         inventory.EndDrag();
    21.  
    22.         // This I hope will instantiate an item for me, but it returns "Nullreferenceexception: Objects reference not set..."
    23.         if(this.gameObject.name.StartsWith("Hot"))
    24.         {
    25.             GameObject inHand = Instantiate(inventory.draggingItem.item3D, transform.position, Quaternion.identity, this.transform) as GameObject;
    26.             Debug.Log("Instantiated item");
    27.         }
    28.     }
    Item class:
    Code (CSharp):
    1. [CreateAssetMenu]
    2. public class Item : ScriptableObject
    3. {
    4.     //General
    5.     public string itemName;
    6.     public Sprite itemIcon;
    7.  
    8.     //Stacking
    9.     public bool isStackable;
    10.     public int maxStackAmount;
    11.  
    12.     // the GO field I populate in inspector
    13.     public GameObject item3D;
    14.  
    15. }
    Inventory script:
    Code (CSharp):
    1.  
    2.  
    3. public class Inventory : MonoBehaviour
    4. {
    5.     public List<GameObject> slots = new List<GameObject>();
    6.  
    7.     [HideInInspector]
    8.     public bool isDragging = false;
    9.     public Image draggingImage = null;
    10.     [HideInInspector]
    11.     public Item draggingItem;
    12.     [HideInInspector]
    13.     public int draggingAmount;
    14.     private GameObject draggingItem3D;
    15.  
    16. // the AddItem method referred to in slot script
    17.     public bool AddItem(Item itemToAdd, int amount)
    18.     {
    19.         Slot emptySlot = null;
    20.         for (int i = 0; i < slots.Count; i++)
    21.         {
    22.             Slot currentSlot = slots[i].GetComponent<Slot>();
    23.             if (currentSlot.myItem == itemToAdd && itemToAdd.isStackable && currentSlot.myAmount + amount <= itemToAdd.maxStackAmount)
    24.             {
    25.                 currentSlot.AddItem(itemToAdd, amount);
    26.                 return true;
    27.             }
    28.             else if (currentSlot.myItem == null && emptySlot == null)
    29.             {
    30.                 emptySlot = currentSlot;
    31.             }
    32.         }
    33.  
    34.         if (emptySlot != null)
    35.         {
    36.             emptySlot.AddItem(itemToAdd, amount);
    37.             return true;
    38.         }
    39.         else
    40.         {
    41.             print("Inventory is full!!!");
    42.             return false;
    43.         }
    44.     }
    45.  
    46.     public void DoDrag(Item itemToDrag, int amnt)
    47.     {
    48.         draggingItem = itemToDrag;
    49.  
    50.         isDragging = true;
    51.         draggingImage.enabled = true;
    52.         draggingImage.sprite = draggingItem.itemIcon;
    53.         draggingAmount = amnt;
    54.         draggingItem3D = draggingItem.item3D;
    55.     }
    56.  
    57.     public void EndDrag()
    58.     {
    59.         draggingItem = null;
    60.         isDragging = false;
    61.         draggingImage.enabled = false;
    62.         draggingAmount = 0;
    63.     }
    64. }
     
  2. egartnuc

    egartnuc

    Joined:
    Jun 2, 2018
    Posts:
    23
    AAAaand I solved it. I created a new method InstGO()" and put the "if(stringstarts..."Hot" and instantiate code there, then I called that method from within AddItem.

    I S*** you not. 2 days. If you read, thanks.