Forum rules - please read before posting.

Highlight when an inventory item is on a Hotspot

KteKte
edited November 2022 in Technical Q&A

Hello,

I have an inventory item that needs to be drag and dropped onto a hotspot.
This hotspot is a specific location on a TV screen and I would like the TV screen to light up when this item hovers over the area.
If I use "Object to Highlight" of the target Hotspot, it works, but any item triggers it.
I would like this to only happen when it is the right item, is this possible?

Thanks.

Comments

  • edited December 2022

    You can attach a custom script that hooks into the OnInventorySelect / OnInventoryDeselect custom events to have the Hotspot automatically turn off when any Inventory item except the one in question is selected (and turn on again when deselected):

    using UnityEngine;
    using AC;
    
    public class AutoDisableHotspot : MonoBehaviour
    {
    
        public Hotspot hotspot;
        public int itemID;
    
        private void OnEnable ()
        {
            EventManager.OnInventorySelect += OnInventorySelect;
            EventManager.OnInventoryDeselect += OnInventoryDeselect;
        }
    
        private void OnDisable ()
        {
            EventManager.OnInventorySelect -= OnInventorySelect;
            EventManager.OnInventoryDeselect -= OnInventoryDeselect;
        }
    
        private void OnInventorySelect (InvItem invItem)
        {
            if (itemID != invItem.id)
            {
                hotspot.TurnOff ();
            }
        }
    
        private void OnInventoryDeselect (InvItem invItem)
        {
            hotspot.TurnOn ();
        }
    
    }
    
  • Thanks Chris, but I have 4 CS0123 errors:
    No overload for 'OnInventoryDeselect' matches delegate 'EventManager.Delegate_Inventory'
    on the instructions:
    EventManager.OnInventorySelect += OnInventorySelect;

  • Sorry, that's my fault.

    I've corrected the script above - give it another try.

  • Thanks Chris,
    By changing the first hotspot.TurnOn(); in TurnOff, it works:
    When the desired item flies over the area, the TV lights up, when it is another item, the TV does not light up.
    But when the empty cursor hovers over the area the TV lights up too, , even if the inventory is empty, that's a problem.

  • Here's an alternative script that will turn the Hotspot on only when the specific item is selected:

    using UnityEngine;
    using AC;
    
    public class AutoDisableHotspot : MonoBehaviour
    {
    
        public Hotspot hotspot;
        public int itemID;
    
        private void Start ()
        {
            hotspot.TurnOff ();
        }
    
        private void OnEnable ()
        {
            EventManager.OnInventorySelect += OnInventorySelect;
            EventManager.OnInventoryDeselect += OnInventoryDeselect;
        }
    
        private void OnDisable ()
        {
            EventManager.OnInventorySelect -= OnInventorySelect;
            EventManager.OnInventoryDeselect -= OnInventoryDeselect;
        }
    
        private void OnInventorySelect (InvItem invItem)
        {
            if (itemID == invItem.id)
            {
                hotspot.TurnOn ();
            }
        }
    
        private void OnInventoryDeselect (InvItem invItem)
        {
            hotspot.TurnOff ();
        }
    
    }
    
  • Thanks Chris, it works perfectly!

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.