Problem is not reproducible or outdated
Inventory help.
I made an inventory script, but the problem is that whenever I try to move an item to a slot behind the item it just sends it back to its original position.
Here's the SlotController class (C#) using UnityEngine; using System.Collections;
public class SlotController : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnMouseHover(){
transform.parent.GetComponent<InventoryController> ().selectedSlot = this.transform;
}
void OnMouseExit(){
transform.parent.GetComponent<InventoryController> ().selectedSlot = null;
}
}
Here's the Item Class (C#) using UnityEngine; using System.Collections;
public class Item : MonoBehaviour {
public string name;
public enum Type {equipment, consumable, weapon, tool, misc};
public Type type;
public Sprite sprite;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void OnMouseHover() {
transform.parent.parent.GetComponent<InventoryController> ().selectedItem = this.transform; }
}
Here's the InventoryController Class (C#) using UnityEngine; using System.Collections; using UnityEngine.UI;
public class InventoryController : MonoBehaviour {
public Transform selectedItem, selectedSlot, originalSlot;
public GameObject slotPrefab, itemPrefab;
public Vector2 inventorySize = new Vector2 (7, 5);
public float slotSize;
public Vector2 WindowSize;
// Use this for initialization
void Start () {
for(int x = 1; x <= inventorySize.x; x++){
for(int y = 1; y <= inventorySize.y; y++){
GameObject slot = Instantiate (slotPrefab) as GameObject;
slot.transform.parent = this.transform;
slot.name = "slot_"+x+"_"+y;
slot.GetComponent<RectTransform>().anchoredPosition = new Vector3(WindowSize.x/(inventorySize.x+1)*x, WindowSize.y /(inventorySize.y+1) * -y, 0);
if(x + (y - 1)*4 <= GameDB.itemList.Count) {
GameObject item = Instantiate (itemPrefab) as GameObject;
item.transform.parent = slot.transform;
item.GetComponent<RectTransform>().anchoredPosition = Vector3.zero;
Item i = item.GetComponent<Item>();
//Item Component Variables
i.name = GameDB.itemList[(x + (y - 1)*4) - 1].name;
i.type = GameDB.itemList[(x + (y - 1)*4) - 1].type;
i.sprite = GameDB.itemList[(x + (y - 1)*4) - 1].sprite;
item.name = i.name;
item.GetComponent<Image>().sprite = i.sprite;
}
}
}
}
// Update is called once per frame
void Update () {
if(Input.GetMouseButtonDown(0) && selectedItem != null){
originalSlot = selectedItem.parent;
selectedItem.GetComponent<Collider>().enabled = false;
}
if (Input.GetMouseButton (0) && selectedItem != null) {
selectedItem.position = Input.mousePosition;
}
else if(Input.GetMouseButtonUp(0) && selectedItem != null){
if(selectedSlot == null) selectedItem.parent = originalSlot;
else{
selectedItem.parent = selectedSlot;
}
selectedItem.localPosition = Vector3.zero;
selectedItem.GetComponent<Collider>().enabled = true;
}
}
}
using UnityEngine; using System.Collections; using UnityEngine.UI;
public class InventoryController : MonoBehaviour {
public Transform selectedItem, selectedSlot, originalSlot;
public GameObject slotPrefab, itemPrefab;
public Vector2 inventorySize = new Vector2 (7, 5);
public float slotSize;
public Vector2 WindowSize;
// Use this for initialization
void Start () {
for(int x = 1; x <= inventorySize.x; x++){
for(int y = 1; y <= inventorySize.y; y++){
GameObject slot = Instantiate (slotPrefab) as GameObject;
slot.transform.parent = this.transform;
slot.name = "slot_"+x+"_"+y;
slot.GetComponent<RectTransform>().anchoredPosition = new Vector3(WindowSize.x/(inventorySize.x+1)*x, WindowSize.y /(inventorySize.y+1) * -y, 0);
if(x + (y - 1)*4 <= GameDB.itemList.Count) {
GameObject item = Instantiate (itemPrefab) as GameObject;
item.transform.parent = slot.transform;
item.GetComponent<RectTransform>().anchoredPosition = Vector3.zero;
Item i = item.GetComponent<Item>();
//Item Component Variables
i.name = GameDB.itemList[(x + (y - 1)*4) - 1].name;
i.type = GameDB.itemList[(x + (y - 1)*4) - 1].type;
i.sprite = GameDB.itemList[(x + (y - 1)*4) - 1].sprite;
item.name = i.name;
item.GetComponent<Image>().sprite = i.sprite;
}
}
}
}
// Update is called once per frame
void Update () {
if(Input.GetMouseButtonDown(0) && selectedItem != null){
originalSlot = selectedItem.parent;
selectedItem.GetComponent<Collider>().enabled = false;
}
if (Input.GetMouseButton (0) && selectedItem != null) {
selectedItem.position = Input.mousePosition;
}
else if(Input.GetMouseButtonUp(0) && selectedItem != null){
if(selectedSlot == null) selectedItem.parent = originalSlot;
else{
selectedItem.parent = selectedSlot;
}
selectedItem.localPosition = Vector3.zero;
selectedItem.GetComponent<Collider>().enabled = true;
}
}
}
And finally here is the GameDB Class (C#) using UnityEngine; using System.Collections; using System.Collections.Generic;
public class GameDB : MonoBehaviour {
public Sprite[] sprites;
public static List<Item> itemList = new List<Item>();
void Start () {
//Item Creation
Item I1 = new Item ();
I1.name = "Baseball Bat";
I1.type = Item.Type.weapon;
I1.sprite = sprites[1];
itemList.Add (I1);
//Item Creation
Item I2 = new Item ();
I2.name = "Torch";
I2.type = Item.Type.tool;
I2.sprite = sprites[2];
itemList.Add (I2);
//Item Creation
Item I3 = new Item ();
I3.name = "Lumber Axe";
I3.type = Item.Type.weapon;
I3.sprite = sprites[3];
itemList.Add (I3);
//Item Creation
Item I4 = new Item ();
I4.name = "Fire Axe";
I4.type = Item.Type.weapon;
I4.sprite = sprites[4];
itemList.Add (I4);
//Item Creation
Item I5 = new Item ();
I5.name = "Pipe Wrench";
I5.type = Item.Type.weapon;
I5.sprite = sprites[5];
itemList.Add (I5);
}
void Update () {
}
}
Follow this Question
Related Questions
Argument out of range? 2 Answers
How to make a Inventory Hotbar 0 Answers
How Can I make items inventory? (HELP) 0 Answers
Adding a prefab to the Item class instance 0 Answers
IPointerEnterHandler and IPointerExitHandler not working 1 Answer