Page 1 of 1

Obtain Usable Information

Posted: Mon Jan 31, 2022 10:40 pm
by PayasoPrince
Hello,

I would like to add a new OnSelectedUsableI() event to my selector. I would like to get the gameobject that is currently selected and store it into a gameobject.

Is there a method already created that I can use to reference the usable object that is currently selected or will I just need to create new OnTriggerEnter/Exit methods?

Code: Select all

gameobject usableObject;

OnSelectedUsable()
{

usableObject = This is where I would need a method that references the usable that is selected.

}

Re: Obtain Usable Information

Posted: Tue Feb 01, 2022 9:48 am
by Tony Li
Hi,

The ProximitySelector (and Selector) component has a UnityEvent OnSelectedUsable() and a C# event SelectedUsableObject. You can add a script to the ProximitySelector's GameObject that hooks into the C# event. Example:

Code: Select all

public class LogSelectionInfo : MonoBehaviour
{
    private Usable currentSelection = null;
    
    void Awake()
    {
        var proximitySelector = GetComponent<ProximitySelector>();
        proximitySelector.SelectedUsableObject += OnSelectedUsableObject;
        proximitySelector.DeselectedUsableObject += OnDeselectedUsableObject;
    }
    
    void OnSelectedUsableObject(Usable usable)
    {
        currentSelection = usable;
    }
    
    void OnDeselectedUsableObject(Usable usable)
    {
        currentSelection = null;
    }
}
ProximitySelector API reference

Re: Obtain Usable Information

Posted: Tue Feb 01, 2022 11:37 am
by PayasoPrince
I was setting it up the way you provided but actually found a simpler way.

My PlayerController already had a reference to it's attached ProximitySelector. So, I was able to call ProximitySelector.CurrentUsable and it seems to work fine! :mrgreen: