Inventory system problem (Keyboard to Trigger event)

Hey there.

So I managed to make the inventory system I wanted but I am having troubles collecting data from the slot item. Its navigation via buttons but as they clone its a little complicated as everything has to work via code.

The problem i’m facing is that when I made a tooltip to change the description it worked via OnPointerEnter. It constructs the string but when i am trying to use a keyboard key it tends to construct the string on all items.

The OnGUI function is the one that constructs the string on all items

The OnPointerEnter works exactly how I want it to but with the mouse. I need this to work via a keyboard press.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class ItemData : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerEnterHandler
{

    public Item item;
    public int amount;

    private Tooltip Description;
    private Inventory inv;
     public List<Item> items = new List<Item>();

    private void Start()
    {
        inv = GameObject.Find("Inventory").GetComponent<Inventory>();
        Description = inv.GetComponent<Tooltip>();
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
      //      Description.Activate(item);
        }
    }
    void OnGUI()
    {
        // GUILayout.Label("Press Enter To Start Game");
        if (Event.current.Equals(Event.KeyboardEvent("Space")))
        {
            Description.Activate(item);
        }
    }

    public void callfrombutton()
    {
       
    }

    public void OnPointerDown(PointerEventData eventData)
    {
       
    }

    public void OnPointerUp(PointerEventData eventData)
    {
       
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        //Description.Activate(item);
    }
}

Please help I am tearing my hair out on this issue :slight_smile:

Don’t use OnGUI. That’s only for editor extensions now.

If you want help, I think you’ll need to better explain what it is you’re trying to do. If you have a bunch of items on the screen, and they all have this script, then of course they are all going to react to a keypress. How could they not? Which one do you want to react to the keypress, and how do you expect the code to know this?

Oh I see . I am trying to call a function to grab the description from the item to a text box. Well on the ipointerenter the items individually grab the description. I need the same effect but with a key press. I’m sorry if it sounds very vague.

Grab the description from which item? It sounds like there are many such items on the screen. Is that right?

I don’t understand what you mean by which item. There is an a item that is referenced to a database. The item itself shouldn’t matter. I want the selected item or highlighted item when using the keyboard to call a function that simply grabs the information. It sounds so simple to me yet it’s so complicated to set up

Edit: To make more sense of it this is the function Description.Activate(item)

OK, there’s the key bit you never mentioned before. Out of all the times on the screen, one of them is selected/highlighted.

Your code above isn’t checking for that. It just says “if a key is pressed, do this!” Instead it needs to say, “if a key is pressed, and I am highlighted, then do this!”

Okay sorry about that. Thanks for assisting me with this. I’m sorry if my coding skills are not very good but I am trying to understand what i’m doing alot. ( Got to start somewhere )

Here is my attempt at trying to solve the issue except it calls the function similarly it the other function it calls for all of the items

I put this in the script.

   private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space) && EventSystem.current.currentSelectedGameObject)
        {
            Description.Activate(item);
        }
    }

Getting closer. In that code, you’re just checking wehther EventSystem.current.currentSelectedGameObject exists at all (i.e., is not null). You want to check whether it equals this particular object. Try changing that part to EventSystem.current.currentSelectedGameObject == gameObject.

Still having no luck with this… When I add that code it resulted in nothing being returned. Even if I remove the input for the key.

Any idea what maybe wrong

Figured it out this worked. Sorry I had the button on the slot not the item… Thank you so much for your help.

1 Like