IDropHandler to Instantiate object in Gameworld

Hey guys, I am using the Idrag/Drop etc System to manage my inventory. Everything is working fine with it.

Now I have tried to Instantiate an Item once it calls “OnDrop” but I dont know how. Is that even possible?

what I mean would be this:

    public void OnDrop(PointerEventData eventData)
    {
GameObject itemObj = Instantiate(eventdata.name);
}

Its just pseudeo code to show you what I mean. Lets say eventData is a stone in my inventory. Now I would like to drop that stone physically in the gameworld on the ground. Thats basically what I am trying to achieve.

If I missed something on google… Id take that link too :smile:

Edit: I forgot to say: I am trying to achieve that ONLY if my mouse is NOT on the inventory panel while i let lose of my mouse.

To drop outside of the inventory you need to use IEndDragHandler. IDropHandler only works on the object being dropped onto.

Another important piece of information you need is EventSystem.current.IsPointerOverGameObject(), and that OnDrop fires before OnEndDrag.

Last but not least. You can instantiate from anywhere in your code. It only needs to be a MonoBehaviour or access to something that can call instantiate.

Thanks for the reply. I think I understand how it should work, will program it now and test it and let you know :slight_smile: Sounds very reasonable already though ^^

Okay that worked really good.

Now my problem is that I have like plenty of items that would need instanciating.

Lets say ive got 2 swords they are in the folder swords.
I have got 5 stones and they are in the folder stones.
Both folders are in a Resources Folder.

If I now go ahead and

(GameObject)Instantiate(Resources.Load("/stones/" +eventData.pointerDrag.name));

he obviously wont find the swords in that folder.
How would I do that? Could be that I find it on google,if so ill edit this answer.

I created a manager that has an array of all the prefabs, right now I am debating to use Resources folder or Asset Bundles. Either way no matter how you bring them in as long as the item being dragged knows what it is then it should be easy.

I have a function in my manager called Spawn, that takes a string and a position. This then uses the string to get the prefab(Resources/AssetBundle/List of Prefabs) and then uses the position to spawn the object. The spawn method returns the GameObject, so if I need to do anything else to it I have that option.

With what you have as different folders for different types of items, you could create a type value(string/enum/int.) then pass that in with the name of the item so it picks the right folder to search when you want to spawn the object.
Something like:

public GameObject Spawn(string objectName, string objectType, Vector3 position)
{
     //Spawn code here
}

Yeah it works kinda. Ive added a string called path to the Item class and that will do for now so now i go for

itempath + itemname

to actually get the prefab i want. not as elegant as id like it to have… but okay for me in the end :slight_smile:

Thansk for helping out there :slight_smile:

Still trying to find a good solution myself, glad I could get you on the right direction.