Adding slots (prefabs) in a grid to an inventory (canvas) by relative positioning

Hello.

I have an inventory system which consists of a canvas to which a script instantiates a prefab in a grid shape as child objects. Each prefab instance represents a slot. The way I currently have it right now is that I manually positioned to get its x, y coordinates then, in my script, I set the ‘beginning’ position to the coordinates I found manually and place the first instantiated slot there, move the x coord by 100, place the second one… and so on.

This gives me the result I wanted but I was wondering if it’s possible (using the anchors maybe?) to define a point IN the canvas that is (0, 0) and simply adding my slots from there. I’m thinking of adding an empty gameobject at the starting position and then read its position in the script which at least would make it easier if I were to move the starting position of the inventory canvas.

My last solution is to manually create the inventory and place the prefabs myself. This would work fine for now because the inventory is a fixed size but if I ever want to give the ability to expand it this wouldn’t be very helpful.

Thanks!

EDIT: Using Unity 5 and the new UI system. Here’s the important part of the script that places the slots:

	for (int i = 0; i < 5; i++) {
		for (int j = 0; j < 5; j++) {
			// Create an inventory slot and make it a child of Inventory
			GameObject slot = (GameObject)Instantiate(slots);
			Slots.Add (slot);
			Items.Add (new Item()); // Add an empty item to it
			slot.name = "Slot(" + i + "," + j + ")";
                            // Make the slot a child of the inventory canvas
			slot.transform.SetParent(this.gameObject.transform, false);

			// Position it properly
			slot.GetComponent<RectTransform>().localPosition = new Vector3(x, y, 0f);
			x = x + 120;
		}
		x = -1055;
		y -= 120;
	}

Hello Alexstre

have u tried using Grid Layout? it is a component and is located in Layout > Grid Layout Group. every child of this GameObject(with the component) is gonna be aligned in a grid like pattern, so you can use a script that runs a loop to instantiate the prefabs as a child of the gameobject

EDIT: Here is an image.

Regards Faizan