Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by ziptao · Jan 07, 2014 at 11:00 AM · guitextureinventoryswapping

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.

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

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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