Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed Dec 15, 2015 at 12:19 AM by Phunmunkee for the following reason:

Problem is not reproducible or outdated

avatar image
0
Question by Phunmunkee · Nov 10, 2015 at 05:32 PM · inventoryinventory system

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 () {
 
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges