Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Inventory System error trying to move items inside inventory

Discussion in 'Scripting' started by Paykoman, Mar 11, 2015.

  1. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Hi guys im trying an inventory system and it works until i try to move items inside inventory. Right now when i try move them i get this error:

    invalidoperationexception: operation is not valid due the current state of the object System.Collection.Generic.Stack'1[Item].Peek()

    And this are the scripts im using right now. Can anyone help me plz?
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine.UI;
    5.  
    6. public class Inventory : MonoBehaviour
    7. {
    8.     private RectTransform inventoryRect;
    9.  
    10.     private float inventoryWidth, inventoryHight;
    11.  
    12.     public int slots;
    13.  
    14.     public int rows;
    15.  
    16.     public float slotPaddingLeft, slotPaddingTop;
    17.  
    18.     public float slotSize;
    19.  
    20.     public GameObject slotPrefab;
    21.  
    22.     private List<GameObject> allSlots;
    23.  
    24.     private Slot from, to;
    25.  
    26.     private static int emptySlots;
    27.  
    28.     //access emptySlot from another class
    29.     public static int EmptySlots
    30.     {
    31.         get{ return emptySlots;}
    32.         set{ emptySlots = value;}
    33.     }
    34.  
    35.     // Use this for initialization
    36.     void Start ()
    37.     {
    38.         CreateLayout();
    39.     }
    40.    
    41.     // Update is called once per frame
    42.     void Update ()
    43.     {
    44.    
    45.     }
    46.  
    47.     private void CreateLayout()
    48.     {
    49.         allSlots = new List<GameObject>();
    50.  
    51.         emptySlots = slots;
    52.  
    53.         inventoryWidth = (slots / rows) * (slotSize + slotPaddingLeft) + slotPaddingLeft;
    54.  
    55.         inventoryHight = rows * (slotSize + slotPaddingTop) + slotPaddingTop;
    56.  
    57.         inventoryRect = GetComponent<RectTransform>();
    58.  
    59.         inventoryRect.SetSizeWithCurrentAnchors (RectTransform.Axis.Horizontal, inventoryWidth);
    60.         inventoryRect.SetSizeWithCurrentAnchors (RectTransform.Axis.Vertical, inventoryHight);
    61.  
    62.         int columns = slots / rows;
    63.  
    64.         for (int y = 0; y < rows; y++)
    65.         {
    66.             for (int x = 0; x< columns; x++)
    67.             {
    68.                 GameObject newSlot = (GameObject)Instantiate(slotPrefab);
    69.  
    70.                 RectTransform slotRect = newSlot.GetComponent<RectTransform>();
    71.  
    72.                 newSlot.name = "Slot";    //set name of slot
    73.  
    74.                 newSlot.transform.SetParent(this.transform.parent);    //when instantiate tghe slot prefab with set it as child of "this" inventory, child of Canvas, so it dont go out of main Canvas GameObject
    75.  
    76.                 slotRect.localPosition = inventoryRect.localPosition + new Vector3(slotPaddingLeft * (x + 1) + (slotSize * x), -slotPaddingTop * (y + 1) - (slotSize * y));
    77.  
    78.                 slotRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, slotSize);
    79.                 slotRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, slotSize);
    80.  
    81.                 allSlots.Add(newSlot);
    82.             }
    83.         }
    84.     }
    85.  
    86.     public bool AddItem(Item item)
    87.     {
    88.         if (item.maxSize == 1) {
    89.             PlaceEmpty (item);
    90.             return true;
    91.         }
    92.         else
    93.         {
    94.             foreach (GameObject slot in allSlots)
    95.             {
    96.                 Slot tmp = slot.GetComponent<Slot>();
    97.  
    98.                 if(!tmp.IsEmpty)
    99.                 {
    100.                     if(tmp.CurrentItem.type == item.type && tmp.IsAvailable)
    101.                     {
    102.                         tmp.AddItem(item);
    103.                         return true;
    104.                     }
    105.                 }
    106.             }
    107.             if(emptySlots > 0)
    108.             {
    109.                 PlaceEmpty(item);
    110.             }
    111.         }
    112.  
    113.         return false;
    114.     }
    115.  
    116.     //Check the empty slot and place item
    117.     private bool PlaceEmpty(Item item)
    118.     {
    119.         if (emptySlots > 0)
    120.         {
    121.             foreach(GameObject slot in allSlots)
    122.             {
    123.                 Slot tmp = slot.GetComponent<Slot>();
    124.  
    125.                 if(tmp.IsEmpty)
    126.                 {
    127.                     tmp.AddItem(item);
    128.                     emptySlots--;
    129.                     return true;
    130.                 }
    131.             }
    132.         }
    133.  
    134.         return false;
    135.     }
    136.  
    137.     //move items inside inventory
    138.     public void MoveItem(GameObject clicked)
    139.     {
    140.         if (from == null) {
    141.             if (clicked.GetComponent<Slot> ().IsEmpty)
    142.             {
    143.                 from = clicked.GetComponent<Slot> ();
    144.                 from.GetComponent<Image> ().color = Color.gray;
    145.             }
    146.         } else if (to == null)
    147.         {
    148.             to = clicked.GetComponent<Slot>();
    149.         }
    150.         if (to != null && from != null)
    151.         {
    152.             Stack<Item> tmpTo = new Stack<Item>(to.Items);
    153.             to.AddItems(from.Items);
    154.  
    155.             if(tmpTo.Count == 0)
    156.             {
    157.                 from.ClearSlot();
    158.             }
    159.             else
    160.             {
    161.                 from.AddItems(tmpTo);
    162.             }
    163.  
    164.             //reset gray color
    165.             from.GetComponent<Image>().color = Color.white;
    166.             to = null;
    167.             from = null;
    168.         }
    169.     }
    170. }
    171.  
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine.UI;
    5. using UnityEngine.EventSystems;
    6.  
    7. public class Slot : MonoBehaviour, IPointerClickHandler
    8. {
    9.     private Stack<Item> items;
    10.  
    11.     public Stack<Item> Items
    12.     {
    13.         get{ return items;}
    14.         set{ items = value; }
    15.     }
    16.  
    17.     public Text stackText;
    18.  
    19.     public Sprite slotEmpty;
    20.  
    21.     public Sprite slotHighlight;
    22.  
    23.     public bool IsEmpty
    24.     {
    25.         get{return items.Count == 0;}
    26.     }
    27.  
    28.     public bool IsAvailable
    29.     {
    30.         get{ return CurrentItem.maxSize > items.Count;}
    31.     }
    32.  
    33.     public Item CurrentItem
    34.     {
    35.         get{ return items.Peek(); }
    36.     }
    37.  
    38.     // Use this for initialization
    39.     void Start ()
    40.     {
    41.         items = new Stack<Item>();
    42.         RectTransform slotRect = GetComponent<RectTransform>();
    43.         RectTransform textRect = stackText.GetComponent<RectTransform>();
    44.  
    45.         int textScaleFactor = (int)(slotRect.sizeDelta.x * 0.60);
    46.         stackText.resizeTextMaxSize = textScaleFactor;
    47.         stackText.resizeTextMinSize = textScaleFactor;
    48.  
    49.         textRect.SetSizeWithCurrentAnchors (RectTransform.Axis.Horizontal, slotRect.sizeDelta.x);
    50.         textRect.SetSizeWithCurrentAnchors (RectTransform.Axis.Vertical, slotRect.sizeDelta.y);
    51.     }
    52.    
    53.     // Update is called once per frame
    54.     void Update ()
    55.     {
    56.    
    57.     }
    58.  
    59.     public void AddItem(Item item)
    60.     {
    61.         items.Push (item);
    62.  
    63.         if (items.Count > 1)
    64.         {
    65.             stackText.text = items.Count.ToString();
    66.         }
    67.  
    68.         ChangeSprite (item.spriteNeutral, item.spriteHighlighted);
    69.     }
    70.  
    71.     //change the stack of items position
    72.     public void AddItems(Stack<Item> items)
    73.     {
    74.         //replace stack item
    75.         this.items = new Stack<Item> (items);
    76.  
    77.         //change the number of items
    78.         stackText.text = items.Count > 1 ? items.Count.ToString() : string.Empty;
    79.  
    80.         //change sprites
    81.         ChangeSprite (CurrentItem.spriteNeutral, CurrentItem.spriteHighlighted);
    82.  
    83.     }
    84.  
    85.     private void ChangeSprite(Sprite neutral, Sprite highlight)
    86.     {
    87.         GetComponent<Image>().sprite = neutral;
    88.  
    89.         SpriteState st = new SpriteState();
    90.         st.highlightedSprite = highlight;
    91.         st.pressedSprite = neutral;
    92.  
    93.         GetComponent<Button>().spriteState = st;
    94.     }
    95.  
    96.     //Use Items
    97.     private void UseItem()
    98.     {
    99.         if (!IsEmpty)
    100.         {
    101.             items.Pop ().Use();    //takes top item of slot and use it
    102.  
    103.             stackText.text = items.Count > 1 ? items.Count.ToString() : string.Empty;
    104.  
    105.             if(IsEmpty)
    106.             {
    107.                 ChangeSprite(slotEmpty, slotHighlight);
    108.  
    109.                 Inventory.EmptySlots++;
    110.             }
    111.         }
    112.     }
    113.  
    114.     //If nothing to trade get empty
    115.     public void ClearSlot()
    116.     {
    117.         items.Clear ();
    118.         ChangeSprite (slotEmpty, slotHighlight);
    119.         stackText.text = string.Empty;
    120.     }
    121.  
    122.     //click the item with right mouse button, need implementation after monoBehavior
    123.     public void OnPointerClick(PointerEventData eventData)
    124.     {
    125.         if (eventData.button == PointerEventData.InputButton.Right)
    126.         {
    127.             UseItem();
    128.         }
    129.     }
    130. }
    131.  
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public enum ItemType{MANA, HEALTH};
    5.  
    6. public class Item : MonoBehaviour
    7. {
    8.     public ItemType type;
    9.  
    10.     public Sprite spriteNeutral;
    11.  
    12.     public Sprite spriteHighlighted;
    13.  
    14.     public int maxSize;
    15.  
    16.     public void Use()
    17.     {
    18.         switch (type)
    19.         {
    20.             case ItemType.MANA:
    21.                 Debug.Log("I just used a mana potion");
    22.                 break;
    23.             case ItemType.HEALTH:
    24.                 Debug.Log("I just used a health potion");
    25.                 break;
    26.         }
    27.     }
    28.  
    29. }
    30.  
     
  2. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Ok i solve my problem but now i get some other maybe can someone help me.

    My inventory is done i can pick items, use them but i try to move them inside the inventory and right now i cant. I dont get any error in my scripts and hv other problems... since i use click to move my player right now when i open inventary and click to drag the items inside my character moveand because of that i cant swap my items and my character run everywhere when thats supposed to when mouse over the inventory window dont affect the behind floor in that place just arround inventary.. This is my scripts right now.

    Appreciate any help.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine.UI;
    5. using UnityEngine.EventSystems;
    6.  
    7. public class Inventory : MonoBehaviour
    8. {
    9.     private RectTransform inventoryRect;
    10.  
    11.     private float inventoryWidth, inventoryHight;
    12.  
    13.     public int slots;
    14.  
    15.     public int rows;
    16.  
    17.     public float slotPaddingLeft, slotPaddingTop;
    18.  
    19.     public float slotSize;
    20.  
    21.     public GameObject slotPrefab;
    22.  
    23.     private List<GameObject> allSlots;
    24.  
    25.     public GameObject iconPrefab;
    26.  
    27.     private static GameObject hoverObject;
    28.  
    29.     private static Slot from, to;
    30.  
    31.     private static int emptySlots;
    32.  
    33.     public Canvas canvas;
    34.  
    35.     private float hoverYOffset;
    36.  
    37.     public EventSystem eventSystem;
    38.  
    39.     //access emptySlot from another class
    40.     public static int EmptySlots
    41.     {
    42.         get{ return emptySlots;}
    43.         set{ emptySlots = value;}
    44.     }
    45.  
    46.     // Use this for initialization
    47.     void Start ()
    48.     {
    49.         CreateLayout ();
    50.  
    51.     }
    52.    
    53.     // Update is called once per frame
    54.     void Update ()
    55.     {
    56.         if (Input.GetMouseButtonUp (0))
    57.         {
    58.             if(eventSystem.IsPointerOverGameObject(-1) && from != null)
    59.             {
    60.                 from.GetComponent<Image>().color = Color.white;
    61.                 from.ClearSlot();
    62.                 Destroy(GameObject.Find ("Hover"));
    63.                 to = null;
    64.                 from = null;
    65.                 hoverObject = null;
    66.             }
    67.         }
    68.  
    69.         if (hoverObject != null)
    70.         {
    71.             Vector2 position;
    72.             RectTransformUtility.ScreenPointToLocalPointInRectangle (canvas.transform as RectTransform, Input.mousePosition, canvas.worldCamera, out position);
    73.             position.Set(position.x, position.y - hoverYOffset);
    74.             hoverObject.transform.position = canvas.transform.TransformPoint (position);
    75.         }
    76.     }
    77.  
    78.     private void CreateLayout()
    79.     {
    80.         allSlots = new List<GameObject>();
    81.  
    82.         hoverYOffset = slotSize * 0.01f;
    83.  
    84.         emptySlots = slots;
    85.  
    86.         inventoryWidth = (slots / rows) * (slotSize + slotPaddingLeft) + slotPaddingLeft;
    87.  
    88.         inventoryHight = rows * (slotSize + slotPaddingTop) + slotPaddingTop;
    89.  
    90.         inventoryRect = GetComponent<RectTransform>();
    91.  
    92.         inventoryRect.SetSizeWithCurrentAnchors (RectTransform.Axis.Horizontal, inventoryWidth);
    93.         inventoryRect.SetSizeWithCurrentAnchors (RectTransform.Axis.Vertical, inventoryHight);
    94.  
    95.         int columns = slots / rows;
    96.  
    97.         for (int y = 0; y < rows; y++)
    98.         {
    99.             for (int x = 0; x< columns; x++)
    100.             {
    101.                 GameObject newSlot = (GameObject)Instantiate(slotPrefab);
    102.  
    103.                 RectTransform slotRect = newSlot.GetComponent<RectTransform>();
    104.  
    105.                 newSlot.name = "Slot";    //set name of slot
    106.  
    107.                 newSlot.transform.SetParent(this.transform.parent);    //when instantiate tghe slot prefab with set it as child of "this" inventory, child of Canvas, so it dont go out of main Canvas GameObject
    108.  
    109.                 slotRect.localPosition = inventoryRect.localPosition + new Vector3(slotPaddingLeft * (x + 1) + (slotSize * x), -slotPaddingTop * (y + 1) - (slotSize * y));
    110.  
    111.                 slotRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, slotSize);
    112.                 slotRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, slotSize);
    113.  
    114.                 allSlots.Add(newSlot);
    115.             }
    116.         }
    117.     }
    118.  
    119.     public bool AddItem(Item item)
    120.     {
    121.         if (item.maxSize == 1) {
    122.             PlaceEmpty (item);
    123.             return true;
    124.         }
    125.         else
    126.         {
    127.             foreach (GameObject slot in allSlots)
    128.             {
    129.                 Slot tmp = slot.GetComponent<Slot>();
    130.  
    131.                 if(!tmp.IsEmpty)
    132.                 {
    133.                     if(tmp.CurrentItem.type == item.type && tmp.IsAvailable)
    134.                     {
    135.                         tmp.AddItem(item);
    136.                         return true;
    137.                     }
    138.                 }
    139.             }
    140.             if(emptySlots > 0)
    141.             {
    142.                 PlaceEmpty(item);
    143.             }
    144.         }
    145.  
    146.         return false;
    147.     }
    148.  
    149.     //Check the empty slot and place item
    150.     private bool PlaceEmpty(Item item)
    151.     {
    152.         if (emptySlots > 0)
    153.         {
    154.             foreach(GameObject slot in allSlots)
    155.             {
    156.                 Slot tmp = slot.GetComponent<Slot>();
    157.  
    158.                 if(tmp.IsEmpty)
    159.                 {
    160.                     tmp.AddItem(item);
    161.                     emptySlots--;
    162.                     return true;
    163.                 }
    164.             }
    165.         }
    166.  
    167.         return false;
    168.     }
    169.  
    170.     //move items inside inventory
    171.     public void MoveItem(GameObject clicked)
    172.     {
    173.         if (from == null) {
    174.             if (!clicked.GetComponent<Slot> ().IsEmpty)
    175.             {
    176.                 from = clicked.GetComponent<Slot> ();
    177.                 from.GetComponent<Image> ().color = Color.gray;
    178.  
    179.                 //item move in cursor position in inventory
    180.                 hoverObject = (GameObject)Instantiate(iconPrefab);
    181.                 hoverObject.GetComponent<Image>().sprite = clicked.GetComponent<Image>().sprite;
    182.                 hoverObject.name = "Hover";
    183.  
    184.                 RectTransform hoverTransform = hoverObject.GetComponent<RectTransform>();
    185.                 RectTransform clickedTransform = clicked.GetComponent<RectTransform>();
    186.  
    187.                 hoverTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, clickedTransform.sizeDelta.x);
    188.                 hoverTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, clickedTransform.sizeDelta.y);
    189.  
    190.                 hoverObject.transform.SetParent(GameObject.Find("Canvas").transform, true);
    191.  
    192.                 hoverObject.transform.localScale = from.gameObject.transform.localScale;
    193.             }
    194.         } else if (to == null)
    195.         {
    196.             to = clicked.GetComponent<Slot>();
    197.             Destroy(GameObject.Find ("Hover"));
    198.         }
    199.         if (to != null && from != null)
    200.         {
    201.             Stack<Item> tmpTo = new Stack<Item>(to.Items);
    202.             to.AddItems(from.Items);
    203.  
    204.             if(tmpTo.Count == 0)
    205.             {
    206.                 from.ClearSlot();
    207.             }
    208.             else
    209.             {
    210.                 from.AddItems(tmpTo);
    211.             }
    212.  
    213.             //reset gray color
    214.             from.GetComponent<Image>().color = Color.white;
    215.             to = null;
    216.             from = null;
    217.             hoverObject = null;
    218.         }
    219.     }
    220. }
    221.  
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine.UI;
    5. using UnityEngine.EventSystems;
    6.  
    7. public class Slot : MonoBehaviour, IPointerClickHandler
    8. {
    9.     private Stack<Item> items;
    10.  
    11.     public Stack<Item> Items
    12.     {
    13.         get{ return items;}
    14.         set{ items = value; }
    15.     }
    16.  
    17.     public Text stackText;
    18.  
    19.     public Sprite slotEmpty;
    20.  
    21.     public Sprite slotHighlight;
    22.  
    23.     public bool IsEmpty
    24.     {
    25.         get{return items.Count == 0;}
    26.     }
    27.  
    28.     public bool IsAvailable
    29.     {
    30.         get{ return CurrentItem.maxSize > items.Count;}
    31.     }
    32.  
    33.     public Item CurrentItem
    34.     {
    35.         get{ return items.Peek(); }
    36.     }
    37.  
    38.     // Use this for initialization
    39.     void Start ()
    40.     {
    41.         items = new Stack<Item>();
    42.         RectTransform slotRect = GetComponent<RectTransform>();
    43.         RectTransform textRect = stackText.GetComponent<RectTransform>();
    44.  
    45.         int textScaleFactor = (int)(slotRect.sizeDelta.x * 0.60);
    46.         stackText.resizeTextMaxSize = textScaleFactor;
    47.         stackText.resizeTextMinSize = textScaleFactor;
    48.  
    49.         textRect.SetSizeWithCurrentAnchors (RectTransform.Axis.Horizontal, slotRect.sizeDelta.x);
    50.         textRect.SetSizeWithCurrentAnchors (RectTransform.Axis.Vertical, slotRect.sizeDelta.y);
    51.     }
    52.    
    53.     // Update is called once per frame
    54.     void Update ()
    55.     {
    56.    
    57.     }
    58.  
    59.     public void AddItem(Item item)
    60.     {
    61.         items.Push (item);
    62.  
    63.         if (items.Count > 1)
    64.         {
    65.             stackText.text = items.Count.ToString();
    66.         }
    67.  
    68.         ChangeSprite (item.spriteNeutral, item.spriteHighlighted);
    69.     }
    70.  
    71.     //change the stack of items position
    72.     public void AddItems(Stack<Item> items)
    73.     {
    74.         //replace stack item
    75.         this.items = new Stack<Item> (items);
    76.  
    77.         //change the number of items
    78.         stackText.text = items.Count > 1 ? items.Count.ToString() : string.Empty;
    79.  
    80.         //change sprites
    81.         ChangeSprite (CurrentItem.spriteNeutral, CurrentItem.spriteHighlighted);
    82.  
    83.     }
    84.  
    85.     private void ChangeSprite(Sprite neutral, Sprite highlight)
    86.     {
    87.         GetComponent<Image>().sprite = neutral;
    88.  
    89.         SpriteState st = new SpriteState();
    90.         st.highlightedSprite = highlight;
    91.         st.pressedSprite = neutral;
    92.  
    93.         GetComponent<Button>().spriteState = st;
    94.     }
    95.  
    96.     //Use Items
    97.     private void UseItem()
    98.     {
    99.         if (!IsEmpty)
    100.         {
    101.             items.Pop ().Use();    //takes top item of slot and use it
    102.  
    103.             stackText.text = items.Count > 1 ? items.Count.ToString() : string.Empty;
    104.  
    105.             if(IsEmpty)
    106.             {
    107.                 ChangeSprite(slotEmpty, slotHighlight);
    108.  
    109.                 Inventory.EmptySlots++;
    110.             }
    111.         }
    112.     }
    113.  
    114.     //If nothing to trade get empty
    115.     public void ClearSlot()
    116.     {
    117.         items.Clear ();
    118.         ChangeSprite (slotEmpty, slotHighlight);
    119.         stackText.text = string.Empty;
    120.     }
    121.  
    122.     //click the item with right mouse button, need implementation after monoBehavior
    123.     public void OnPointerClick(PointerEventData eventData)
    124.     {
    125.         if (eventData.button == PointerEventData.InputButton.Right)
    126.         {
    127.             UseItem();
    128.         }
    129.     }
    130. }
    131.  
    132.  
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public enum ItemType{MANA, HEALTH};
    5.  
    6. public class Item : MonoBehaviour
    7. {
    8.     public ItemType type;
    9.  
    10.     public Sprite spriteNeutral;
    11.  
    12.     public Sprite spriteHighlighted;
    13.  
    14.     public int maxSize;
    15.  
    16.     public void Use()
    17.     {
    18.         switch (type)
    19.         {
    20.             case ItemType.MANA:
    21.                 Debug.Log("I just used a mana potion");
    22.                 break;
    23.             case ItemType.HEALTH:
    24.                 Debug.Log("I just used a health potion");
    25.                 break;
    26.         }
    27.     }
    28.  
    29. }
    30.  
    31.  
    and the player move scripts is this one

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ClickToMove : MonoBehaviour
    5. {
    6.     NavMeshAgent navAgent;
    7.  
    8.     Animation animations;
    9.  
    10.     public AnimationClip idleAnimation;
    11.     public AnimationClip runAnimation;
    12.  
    13.     void Awake()
    14.     {
    15.         navAgent = GetComponent<NavMeshAgent>();
    16.         animations = GetComponent<Animation>();
    17.     }
    18.    
    19.     void Update()
    20.     {
    21.         Move();
    22.         Animate();
    23.     }
    24.  
    25.     //This method handles the movement of player
    26.     //The player moves to the point where the user clicks in terrain
    27.     void Move()
    28.     {
    29.         RaycastHit hit;
    30.         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    31.        
    32.         if (Input.GetMouseButton (0))
    33.         {
    34.             if (Physics.Raycast (ray, out hit, 100))
    35.             {
    36.                 navAgent.SetDestination(hit.point);
    37.             }
    38.         }
    39.     }
    40.  
    41.     void Animate()
    42.     {
    43.         if (!Player.isAttaking)
    44.         {
    45.             if (navAgent.velocity.magnitude > 0.5f)
    46.             {
    47.                 animations.CrossFade (runAnimation.name);      
    48.             }
    49.             else
    50.             {
    51.                 animations.CrossFade (idleAnimation.name);
    52.             }
    53.         }
    54.     }
    55. }
    56.  
     
  3. Cherno

    Cherno

    Joined:
    Apr 7, 2013
    Posts:
    515
    Assuming you use OnGUI for your inventory GUI, use HotControl and Event.current use:

    Code (csharp):
    1.  
    2. if(GUI.Button(buttonRect, "Button")) {
    3.                 if (Event.current.button == 0 && GUIUtility.hotControl == 0) {
    4.                     Event.current.Use();
    5.                    //your code on button press
    6.  
    Also, to make things more watertight, you can do the following:

    Whenever the mouse cursor is inside any GUI rectangle (using Rect.Contains()), you set a boolean "mouseOverGUI" to true, and in LateUpdate() you set it to false again. Now, whenever you check for mouse clicks inside Update() to move your character, for example, you also check if mouseOverUI is false, and only then do you proceed.
     
    Paykoman likes this.
  4. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Yes im using in GUI system.. Tomorrow i willcheck this and posto some reply ty very much
     
  5. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    I read it wrong last night.. im use the new GUI system.. not the old one... So the problem persist.. Anyway ty
     
  6. Cherno

    Cherno

    Joined:
    Apr 7, 2013
    Posts:
    515
    Anyway, it's a common issue so even with the UI there shoudl be others who had the problem and found a solution. I recommend searching for "mouse click going through GUI" or something like that.
     
    Paykoman likes this.
  7. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    So the point here is this i hv this inventory with some potion, if i right click them i can use them and it assumes the inventory, if i left click item my character starts run everywhere because in my clicktomove script i use left mouse buton to move... And because of that i cant drag the items inside my inventory too..