How do I make things droppable

Updated on October 11, 2017 in Answers
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
7 on October 5, 2017

Hello, I`m making a RPG following Brackeys tutorial. I have come into a problem I want to be able to drop items. I have searched for hours and nothing I have tried and failed. So it would be really appreciated if someone could help me my code is exactly the same as Brackeys.

using UnityEngine;
using UnityEngine.UI;
public class InventorySlot : MonoBehaviour {
public Image icon;
public Button removeButton;
Item item;
public void AddItem (Item newItem)
{
item = newItem;
icon.sprite = item.icon;
icon.enabled = true;
removeButton.interactable = true;
}
public void ClearSlot ()
{ 
item = null;
icon.sprite = null;
icon.enabled = false;
removeButton.interactable = false;
}
public void OnRemoveButton ()
{ 
Inventory.instance.Remove(item);
}
public void UseItem ()
{
if (item != null)
{
item.Use();
}
}
}

  • Liked by
Reply
4 on October 5, 2017

Hello,

first things first, please don’t like your own comments.

Second, I am pretty sure that I saw that in “Making a RPG in Unity” from Brackeys.

https://www.youtube.com/user/Brackeys/videos

It’s probably in Episode 6 or 7.

on October 5, 2017

No he said that you could make them drop items but he never showed it. And sorry I stopped liking my comment 🙂

Helpful
on October 5, 2017

So the item drop means, that you have to instantiate that object if you had removed it from your inventory. I am not really sure, but you can try this:

public void OnRemoveButton ()
{ 
Inventory.instance.Remove(item);  
//drop on ground but randomly around player
Vector3 randomDropPos = transform.position + new Vector3 (Random.Range(-2f,2f), -transform.position.y, Random.Range(-2f,2f));
//create item with random position and rotation
Instantiate(item, randomDropPos, Random.rotation);
}

Probably you can’t use “item” in instantiate, just try to get the gameObject of the item instead.

on October 6, 2017

Yea I have tried to find how to get the gameObject of the item but no luck and Yes that didnt work but thx anyways

Helpful
on October 6, 2017

Then do as “HugoLeblanc” beneath me said. Create gameObjects for each item and instantiate that.

 

Show more replies
  • Liked by
Reply
Cancel
1 on October 6, 2017

You have to make prefabs for your items, (ItemName_Drop for exemple) then you’ll have to instantiate it on ground, same position than character, but outside of character object.

on October 11, 2017

If I do that it creates inventoryslot(clone*)

Show more replies
  • Liked by
Reply
Cancel