- Home /
Inventory only swapping in one direction
I have a problem with my inventory in my game only swapping in one direction when I drag and drop Items on each other. The item that is picked up will stay the same, but the one underneath will change to the correct original item. I also noticed that when I comment out the code block of
  else if (Input.GetMouseButtonUp(0) && transform.parent.GetComponent<UISwapper>().hasItem == true)
 {
 
     transform.guiTexture.texture = transform.parent.GetComponent<UISwapper>().heldItem.guiTexture.texture;
 
 }
located in the inventory mouse hovering script OnMouseOver(), it will work in the opposite direction. The original will change to the destination and the destination will remain the same.
So I think the code needed is there, just in the wrong spot maybe? I've tried everything I can think of to correct the problem but maybe someone else has an idea on what I need to do.
If you need additional information please let me know. I'm using empty gameobjects to hold the code and they have changeable guitextures. I tried using the OnGUI() but it gave me insane FPS drops, and this way doesn't give me any performance issues.
Script Used for when hovering over the object (attached to inventory slots)
 public class InventoryMouseHover : MonoBehaviour {
 
 
     public Transform player;
 
   
     
     void OnMouseOver()
     {
 
 
         if (Input.GetMouseButton(0) && transform.parent.GetComponent<UISwapper>().hasItem == true)
         {
             transform.parent.GetComponent<UISwapper>().replacedItem = transform.guiTexture;
             
         }
 
 
         else if (Input.GetMouseButtonUp(0) && transform.parent.GetComponent<UISwapper>().hasItem == true)
         {
             
            transform.guiTexture.texture = transform.parent.GetComponent<UISwapper>().heldItem.guiTexture.texture;
 
         }
 
        
        
     }
 
 
     void OnMouseExit()
     {
         //reset the destination of the swap to null
         transform.parent.GetComponent<UISwapper>().replacedItem = null;
        
     }
 
 
   
 
    
 }
Script for picking up and moving the item around (attached to inventory slots)
 public class MoveItemInBag : MonoBehaviour {
 
 
     bool pickedUp;
     Vector3 originalPosition;
 
 
     // Use this for initialization
     void Start () {
 
 
         pickedUp = false;
         originalPosition = transform.position;
     
     }
     
     // Update is called once per frame
     void Update () {
 
         if (pickedUp)
         {
             HoldingItem();
 
             if (Input.GetMouseButtonUp(0) && transform.parent.GetComponent<UISwapper>().replacedItem != null)
             {
                 transform.guiTexture.texture = transform.parent.GetComponent<UISwapper>().replacedItem.guiTexture.texture;
                 
 
             }
 
         }
 
         
     
     }
 
     void HoldingItem()
     {
         if (Input.GetMouseButton(0))
         {
             //item is held
 
             transform.parent.GetComponent<UISwapper>().heldItem = transform.guiTexture;
             transform.parent.GetComponent<UISwapper>().hasItem = true;
             Follow();
 
         }
 
         if (Input.GetMouseButtonUp(0))
         {
             //Item no longer being held
 
 
 
             //set inset of guiTexture & position
             transform.guiTexture.pixelInset = new Rect(transform.position.x, transform.position.y, 0, 0);
             transform.position = originalPosition;
 
 
             transform.parent.GetComponent<UISwapper>().heldItem = null;
             transform.parent.GetComponent<UISwapper>().hasItem = false;
 
             pickedUp = false;
         }
     }
 
 
     void Follow()
     {
 
 
         //set position to lower left for screen location accuracy
         transform.position = new Vector2(0, 0);
         //follow mouse                                                                        
         transform.guiTexture.pixelInset = new Rect(Input.mousePosition.x + 2, Input.mousePosition.y + 15, 1, 1);
 
     }
 
 
 
     void OnMouseOver()
     {
 
         if (Input.GetMouseButtonDown(0) && transform.parent.GetComponent<UISwapper>().hasItem == false)
             pickedUp = true;
 
 
 
     }
 }
Script for holding the values for the swapping (parent of all inventory slots)
 public class UISwapper : MonoBehaviour {
 
 
     //public only for testing
     public GUITexture heldItem;
     public GUITexture replacedItem;
    
 
     public bool hasItem;
 
 
     void Start()
     {
 
         heldItem = null;
         replacedItem = null;
         hasItem = false;
         
 
     }
 
 
     
 
 }
I'm a beginner of both scripting and Unity, so this probably isn't the best way to go about things, but I'm just experimenting at the moment. Thanks.
Your answer
 
 
             Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Battery Collect Collision Problem 2 Answers
Array Help GameObject Length Inventory 1 Answer
Space ship UI, multiple pointer that follow individual targets. 1 Answer
UFPS:ultimate fps cameraGUI texture wont show in game 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                