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 06, 2014 at 04:52 PM · inspectorinventorychild-to-gameobjectswapping

Script only working on one child?

Hey all, I'm new to unity and programming in general and I've run into an issue that I can't seem to find the answer to.

I have a parent game object named Inventory with the script UISwapper that I am going to use to switch item guiTextures around in a bag. Every slot of the bag is a child of the inventory parent. I am able to pick up items on the ground, take the texture of that item and apply it to the empty game object bag slot so it shows in the bag. I am also able to pick up and drag the item out of the bag and around the screen. My next task is to be able to switch two items in the bag with drag and drop.

I coded how I want it to work which is shown below, but for some reason it only works with one child and not any of the others. The inspector doesn't update the parent inventory public variables at all, except when the bag slot1 child is held on the mouse and I can't figure out why this is.

I checked to make sure I wasn't specifically naming the bag anywhere, all the code is the same on all the slots and all the public transforms etc are the same. Another strange thing that happened was one time I saved the project and closed/reopened, and only bag slot2 worked, while the others didn't.

I'm wondering if this is just a bug with the inspector or if I'm missing something obvious. I'm sorry if this has been asked before, but I looked everywhere and couldn't find anything similar. If I need to clear anything up let me know, and thanks in advance.

This Script is on every child of the inventory parent.

 public class InventoryMouseFunctions : MonoBehaviour {
 
 
     public Transform player;
 
     Vector3 originalPosition;
 
      bool pickedUp;
 
     
   
     // Use this for initialization
     void Start () {
 
         
         pickedUp = false;
         originalPosition = transform.position;
       
 
     }
     
     // Update is called once per frame
     void Update () {
 
         //cehck if item is being held
         HoldingItem();     
     }
 
 
     void HoldingItem()
     {
         if (Input.GetMouseButton(0) && pickedUp == true)
         {
             //item is held
 
             transform.parent.GetComponent<UISwapper>().HeldTex = transform.guiTexture;
             transform.parent.GetComponent<UISwapper>().IsItemHeld = true;
             transform.parent.GetComponent<UISwapper>().original = transform;
             
             Follow();
         }
 
         else
         {
 
             //Item no longer being held
             pickedUp = false;
             transform.parent.GetComponent<UISwapper>().HeldTex = null;
             transform.parent.GetComponent<UISwapper>().IsItemHeld = false;
             transform.parent.GetComponent<UISwapper>().original = null;
             
             
             //snap back to original position in inventory
             transform.guiTexture.pixelInset = new Rect(transform.position.x, transform.position.y, 0, 0);
 
             transform.position = originalPosition;
         }
     }
 
 
 
 
     void Follow()
     {
         
      
         //set position to lower left
         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()
     {
 
 
         // player wants to pick up the item with the mouse
         if (Input.GetMouseButtonDown(0))
         {
             pickedUp = true;
             
 
         }
 
 
         //Player attepting to switch items in bag
 
         if (transform.parent.GetComponent<UISwapper>().IsItemHeld == true)
         {
             transform.parent.GetComponent<UISwapper>().destination = transform;
            
 
         }
 
 
        
     }
 
 
   
 
    
 }



   


   


This Script is on the inventory Parent

 public class UISwapper : MonoBehaviour {
 
 
     //public only for testing
     public GUITexture heldTex;
     public GUITexture HeldTex
     {
         get { return heldTex; }
         set { heldTex = value; }
     }
 
 
     //public only for testing 
     public bool isItemHeld;
     public bool IsItemHeld
     {
         get { return isItemHeld; }
         set { isItemHeld = value; }
         
     }
 
 
     public Transform original;
     public Transform destination;
 
    
     // Use this for initialization
     void Start () {
 
         isItemHeld = false;
         heldTex = null;
     
     }
 
    
     }



P.S This is my first post on UA so if I broke any posting rules or conventions please let me know and I'll fix that in the future. Thanks again!

Comment
Add comment · Show 1
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
avatar image ziptao · Jan 07, 2014 at 11:52 AM 0
Share

I figured out I was missing an if statement after I re-wrote everything. It should be:

 if(pickedUp)
 HoldingItem();

that seemed to solve it, although I ran into another issue I posted in a separate question.

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

Change Enum Contents 1 Answer

Inventory Programming: Derived Class Issue (C#) 2 Answers

Replacing all textures in a project? 1 Answer

Inventory Management Issue 2 Answers

Inventory only swapping in one direction 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