- Home /
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!
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.
Your answer
Follow this Question
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