I have taken the long path and hard coded a small inventory for 5 weapons.
I can press a key to bring up a panel where I have the inventory.
I can switch between the weapons by using the 1-5 numbers on the keyboard.
now I want to be able to click and select with mouse click.
what should I be looking at here?
The items are basiclly UI images.:
Hello, 
You should use the interface “IPointerClickHandler”:
Then you need to implement “OnPointerClick”. It’s the most straightforward way I think.
And if you are looking for some sort of tab selection system (I think this system is close to your inventory) you can check this nice video:
2 Likes
Hmm.
I created this code and attached it to the image object.
When i click on the image it does not debug or anything
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems; // 1
public class onMouseClick : MonoBehaviour, IPointerClickHandler
{
public playerScript ps;
public void OnPointerClick(PointerEventData eventData)
{
/*
ps.haveRiffle = true;
ps.haveGun = false;
ps.haveBat = false;
ps.haveFlamethrower = false;
// ps.haveKnife = false;
*/
Debug.Log("Click DETECTED ON RIFFLE IMAGE");
}
}
Did you read the documentation link above and follow all the instructions?
Yes.
-I have an Eventsystem in my scene, was automaticlly created. I have many canvas and UI elements.
- I have a PhysicsRaycaster 2D attached to my camera.
- Atached the script to the gameObject. My gameObject is a image UI stacked in under a canvas - Panel.
- I have a box collider on the Gameobject
You didn’t read it close enough.
Image components are UI, which means a PhysicsRaycaster is not needed. Also, a BoxCollider shouldn’t be needed and not sure where you got that from.
That being said, I don’t think either of those would interfere, unless the BoxCollider does.
The first thing I would check is to make sure nothing is blocking your clicks. When in play mode, select the EventSystem object in your scene. Then go to your Game Window and click on something in your inventory. Click and hold and see what the EventSystem says you’re hitting. Let go and see what it says you hit. Also, just remove the BoxCollider for now just in case it’s blocking.
Thank you buddy.
i disabled colliders.
I checked the eventsystem. It seems like whatever i point at or click at does somehow get registrat.
Please see my 1 min video screen dump of my setup.
I would suggest creating an empty scene, adding an image, and then adding your click script to it and see if that works. Then reverse from there if it does to make sure stuff matches. Otherwise, there is also the OnPointerDown and OnPointerUp that I know works. I’ll admit I haven’t used the click one before.
https://docs.unity3d.com/2017.3/Documentation/ScriptReference/UI.Selectable.OnPointerUp.html
Thank you so much.
I will look into this ASAP.
You really don’t know how much I appreciate it.
Cause I am stuck on this part and I don’t want to spam this forum so I have been trying to scavenge the www for a similar issue.
But seems not to many hardcoded it. They use like arrays , loops , buttons etc…
I really hope this works. I mean it works when I switch with the keypads so similar function with mouse should work…
Dude… The OnPointerDown and Enter works perfectly.
Thank you so much…
I am curious why the click method did not work??
also it seems as this pointer down behaves same way as the click should work??
Well, PointerDown is for when you click the button down, and then PointerUp is when you release it. Click should be technically I think just when you click the mouse button (down and up quickly). I’ll be honest, I haven’t ever used the Click one. I’m just use to the PointerDown/PointerUp one.
I’m glad it worked for you!