I’m trying to write a simple 2D game in Unity with an inventory where the user can drag and drop different objects on the screen.

I need the inventory to contain icons which on drag will become different game objects.

I’m struggling in positioning the game object after clicking the icon and using its drag-and-drop functionality (a script component which i attached to it).

I would really appreciate some guidelines or opinions about the design because I feel I’m doing things wrong.. What do you think?



As your question, now clearer seems to be more about design and not so much a technical question I change my answer a bit.

Your first attempt was the right one. Your sprite contains a drag&drop component. But it's necessary that it also contains the prefab it will instantiate once dropped in the game world. The main design is, from an inventory item perspective:

  1. I know what prefab I represent
  2. When player drop me on the scene I get the Mouse position and Instantiate the prefab I represent

You can add a 3rd step if you want the item sprite to its initial position in the inventory (but the inventory item will have to save its initial position before drag&drop begins).

[Original answer]

Let me rephrase your question as I understand it.

So you have an inventory (somewhere on the screen) full of draggable sprites/objects and you want to instantiate a prefab when the player drop it somewhere on the screen. As you seem to have drag/drop behaviour working, the real problem is about instantiating/activating things at the right place on screen.

If that's correct, I'd say the first technique you tried was a good one. (note that the second one, using prefabs, would totally work but it's not as "clean and easy" in my humble opinion).

Let say that the inventory is composed of sprites. When you drop something from your inventory you actually need two important informations: the drop position and the object to instantite.

The position is actually given by the mouse position. As it's a 2D game you're working on you just have to convert the mouse position, given in screen space, in a world space position. It's pretty easy using the Camera.ScreenToWorldPoint method.

Something like :

Vector2 world_position = Camera.main.ScreenToWorldPoint(Input.mousePosition) as Vector2;
// using Vector2 instead of Vector3 is not mandatory even in a 2D game, but I prefer.

So the trick here is to somehow add a property to the sprites of inventory. Simply add a component (on every inventory object), let's call it InventoryItem, which has the prefab you need to instantiate in a public Transform field.

Something like :

public class InventoryItem : Monobehaviour 
   public Transform item_prefab;
   private void Start() 
      if ( item_prefab == null ) 
          Debug.LogError("Item prefab is not valid.");

Now, when player drop an inventory item, it's easy to know what prefab to Instantiate and where to instantiate it.

public void OnItemDrop(GameObject item)
   // Get world position
   Vector2 item_world_position = Camera.main.ScreenToWorldPoint(Input.mousePosition) as Vector2;
   // Get the prefab to instantiate
   Transform item_prefab = item.GetComponent<InventoryItem>().item_prefab;

   // Instantiate it to world position
   Instantiate(item_prefab, item_world_position, new Quaternion());

   // You can do this if you want your item sprite to disappear
   // Destroy(item);

Note that this code is untested... but it might work :)

I hope it helps.

  • \$\begingroup\$ Thanks you, you helped me find the problem. I instantiated my prefab and added it to my inventory list (instead of adding my instance), so it led to all kind of problems with the position and other stuff... \$\endgroup\$ – moria am Oct 1 '14 at 15:27
  • \$\begingroup\$ Glad I helped you. When you have that kind of problem try thinking from your gameobject point of view. Outing words on action often help designing problems. :) \$\endgroup\$ – lvictorino Oct 1 '14 at 18:04

I made my inventory work with an "Item" class that ties actual game objects to their icon, description, etc. - the inventory only deals with "Items" while using an item (placing it, for example, in the game world) would consume one and then create the game object.

I know that's a bit simplistic of an answer, but I felt like sharing my two cents.


Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.