Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 KolaricV12 · Mar 31, 2019 at 10:48 AM · mousebeginnermenuclick

My inventory opens and closes on left mouse click when i don't want it to.

So i was following a tutorial and i made an inventory that opens and closes when i press i, the issue is it also opens and closes whenever i left mouse click and i don't know why.

Here's the code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Inventory : MonoBehaviour {
     public int SlotsX, SlotsY;
     public GUISkin skin;
     public List<Item> inventory = new List<Item>();
     public List<Item> slots = new List<Item>();
     private bool showInventory;
     private ItemDatabase database;
     private bool showTooltip;
     private string tooltip;
 
     private bool draggingitem;
     private Item draggedItem;
 
 
     // Use this for initialization
     void Start () {
         for (int i = 0; i < (SlotsX * SlotsY); i++)
         {
             slots.Add(new Item());
             inventory.Add (new Item ());
         }
         database = GameObject.FindGameObjectWithTag("Item Database").GetComponent<ItemDatabase>();
         AddItem(1);
         AddItem(0); 
         RemoveItem (1);
 
         print (InventoryContains (1));
     }
 
     void Update()
     {
         if (Input.GetButtonDown ("Inventory")) 
         {
             showInventory = !showInventory;
         }
     }
 
     void  OnGUI ()
     {
         tooltip = "";
         GUI.skin = skin;
         if (showInventory)
         {
             DrawInventory ();
             if (showTooltip) 
                 GUI.Box (new Rect (Event.current.mousePosition.x + 15f, Event.current.mousePosition.y, 200, 200), tooltip, skin.GetStyle("Tooltip"));
         
         }
         if (draggingitem)
         {
             GUI.DrawTexture(new Rect(Event.current.mousePosition.x, Event.current.mousePosition.y, 50, 50), draggedItem.itemIcon);
         }
 
     }
     void DrawInventory()
     {
         Event e = Event.current;
         int i = 0;
         for (int y = 0; y < SlotsY; y++) 
         {
             
             for (int x = 0; x < SlotsX; x++)
             {
                 Rect slotRect = new Rect (x * 110, y * 110, 100, 100);
                 GUI.Box (slotRect, "", skin.GetStyle("Slot"));
                 slots[i] = inventory[i];
 
                 if(slots[i].itemName != null)
                 {
                     GUI.DrawTexture(slotRect, slots[i].itemIcon);
                     if (slotRect.Contains(e.mousePosition)) 
                     {
                         tooltip = CreateTooltip (inventory[i]);
                         showTooltip = true;
                         if (e.button == 0 && e.type == EventType.MouseDrag)
                         {
                             draggingitem = true;
                             draggedItem = slots [i];
                         }
                     }
                 }
                 if (tooltip == "")
                 {
                     showTooltip = false;
                 }
                 i++;
             }
         }
     }
     string CreateTooltip(Item item)
     {
         tooltip = "<color=#ffffff>" + item.itemName + "</color>\n\n" +item.itemDesc;
         return tooltip;
     }
 
     void RemoveItem(int id)
     {
         for(int i = 0; i < inventory.Count; i++)
         {
             if(inventory[i].itemID == id)
             {
                 inventory [i] = new Item();
                 break;
             }
         }
     }
 
     void AddItem(int id)
     {
         for (int i = 0; i < inventory.Count; i++) 
         {
             if (inventory [i].itemName == null) 
             {    
                 for (int j = 0; j < database.items.Count; j++)
                 {
                     if (database.items [j].itemID == id) 
                     {
                         inventory [i] = database.items [j];
                     }
                 }    
                 break;        
             }
         }
     }
 
     bool InventoryContains(int id)
     {
         bool result = false;
         for (int i = 0; i < inventory.Count; i++) 
         {
             result = inventory [i].itemID == id;
             if (result)
             {
                 break;
             }
 
         }
         return result;
     }
 }


I have no idea which part of the code is causing this, i just assumed Unity is assigning left mouse click to the function by default because it's not used currently but now i came to the point where it actually is used for something else and the problem persists.

Comment
Add comment · Show 2
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 Chris333 · Apr 05, 2019 at 10:37 AM 0
Share

Hint:

You should use the UI system which was added with version 4.6. OnGUI had several problems.

The new UI

avatar image Chris333 · Apr 05, 2019 at 10:44 AM 0
Share

Could you check the input settings in the editor. $$anonymous$$aybe you have added the left mouse button to the "Inventory" input as an alternative button.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by cs120319992 · Apr 05, 2019 at 10:48 AM

Maybe you appear it from another script?

Comment
Add comment · Share
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

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

123 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 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 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 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 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 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 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

Object menu must be at the center of the object 1 Answer

Can't shoot towards mouse click point 1 Answer

OnMouseUp() Click Display Effect. 1 Answer

Problem getting my model to animate once mouse clicked 0 Answers

C sharp menu problem with bottons 1 Answer


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