- Home /
Question by
unity_P0X207LzI7FZyA · Feb 21, 2019 at 09:22 AM ·
inventorytoggleinventory system
How do i reference if toggle is on or off?
I am creating an inventory for a mobile game, and I have two lines of code. One lets the player use an item in his inventory, the other lets the player pick the item up and move it around. I want there to be a toggle in the corner of the inventory that when it is not active the player can use an item, and when it is active they can move an item around. I am following a tutorial for the inventory, but he i using left and right click. I want to be able to attach both scripts to my inventory slot, and toggle which one is used. This is my code to use item:
Private void UseItem()
{
if (!IsEmpty)
{
Items.Pop().Use();
stackText.text = Items.Count > 1 ? Items.Count.ToString() : string.Empty;
if (IsEmpty)
{
ChangeSprite(slotEmpty, SlotHighlight);
InventoryMenu.EmptySlots++;
}
This is the code for moving an Item:
public void MoveItem(GameObject clicked)
{
if (from == null)
{
if (!clicked.GetComponent<Slots>().IsEmpty)
{
from = clicked.GetComponent<Slots>();
from.GetComponent<Image>().color = Color.grey;
hoverObject = (GameObject)Instantiate(iconPrefab);
hoverObject.GetComponent<Image>().sprite = clicked.GetComponent<Image>().sprite;
hoverObject.name = "Hover";
RectTransform hoverTransform = hoverObject.GetComponent<RectTransform>();
RectTransform clickedTransform = clicked.GetComponent<RectTransform>();
hoverTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, clickedTransform.sizeDelta.x);
hoverTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, clickedTransform.sizeDelta.y);
hoverObject.transform.SetParent(GameObject.Find("Canvas").transform,true);
hoverObject.transform.localScale = from.gameObject.transform.localScale;
}
}
else if (to == null)
{
to = clicked.GetComponent<Slots>();
Destroy(GameObject.Find("Hover"));
}
if (to != null && from != null)
{
Stack<Item> tmpTo = new Stack<Item>(to.Items);
to.AddItems(from.Items);
if (tmpTo.Count == 0)
{
from.ClearSlot();
}
else
{
from.AddItems(tmpTo);
}
from.GetComponent<Image>().color = Color.white;
to = null;
from = null;
hoverObject = null;
}`
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612192140im_/https://answers.unity.com/themes/thub/images/avi.jpg)