Forum rules - please read before posting.

Deactivate GameObject based on Hotspot State

edited May 2023 in Technical Q&A

Hi

I want to have a hierarchy of game objects set to not-active when a Hotspot is off-camera, effectively when the hotspot State goes to Off. I have the following code but the it doesn't get triggered when the State changes of the specified Hotspot. Am I using the correct delegate? The OnEnable/Disable functions are called, but the onAction and offAction functions don't get called.

    public UnityEvent onHotspotOn;
    public UnityEvent onHotspotOff;
    public Hotspot hotspot;

    void OnEnable()
    {
        EventManager.OnHotspotTurnOn += onAction;
        EventManager.OnHotspotTurnOff += offAction;
    }

    void OnDisable()
    {
        EventManager.OnHotspotTurnOn -= onAction;
        EventManager.OnHotspotTurnOff -= offAction;
    }

    void onAction(Hotspot tempHotspot)
    {
        if (hotspot == tempHotspot)
        {
            onHotspotOn?.Invoke();
        }
    }

    void offAction(Hotspot tempHotspot)
    {
        if (hotspot == tempHotspot)
        {
            onHotspotOff?.Invoke();
        }
    }

Comments

  • You've confirmed its not onHotspotOn that's not being called, but onAction? As in, a Debug.Log command never fires regardless of the Hotspot parameter value?

    The AC event-hooking looks correct.

  • I've put a breakpoint on if (hotspot == tempHotspot) and it never triggers. It does at EventManager.OnHotspotTurnOn += onAction; so it would feel that the IF line in doAction is never actioned.

    However, just to test I've put some debug lines in and those debug lines show no results in the console, but the OnEnable and OnDisable both work.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using AC;
    using UnityEngine.Events;
    
    public class enableWithHotspot : MonoBehaviour
    {
    
        public UnityEvent onHotspotOn;
        public UnityEvent onHotspotOff;
        public Hotspot hotspot;
    
        void OnEnable()
        {
            EventManager.OnHotspotTurnOn += onAction;
            EventManager.OnHotspotTurnOff += offAction;
        }
    
        void OnDisable()
        {
            EventManager.OnHotspotTurnOn -= onAction;
            EventManager.OnHotspotTurnOff -= offAction;
        }
    
        void onAction(Hotspot tempHotspot)
        {
            Debug.Log("Hostpot Name On:" + hotspot.name);
            if (hotspot == tempHotspot)
            {
                onHotspotOn?.Invoke();
            }
        }
    
        void offAction(Hotspot tempHotspot)
        {
            Debug.Log("Hostpot Name Off:" + hotspot.name);
            if (hotspot == tempHotspot)
            {
                onHotspotOff?.Invoke();
            }
        }
    }
    
  • The logs fire when testing on my end - the code is fine.

    How/when are you affecting Hotspots? With the Hotspot: Enable or disable Action?

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.