Forum rules - please read before posting.

Custom script to detect double click only when a hotspot is active

Hi,

Is there a way to check if any hotspot is currently active or not inside a custom script?

I have a scene where the navigation is based on camera switching (Movement method is set to "None" in the project settings) and I want to have some hotspots to run an interaction by double clicking and all the others by single clicking.

Currently I achieved the desired result with the bellow script and using the tag "NavHotspot" in the mentioned hotspots and unchecking the "Single 'Use' Interaction" in the Hotspot Options.

The problem is that I get a NullReferenceException when the player double clicks anywhere that there is no hotspot present which I assume is because there is no active hotspot at that time to check the tag.

This must be really simple to solve but I'm stuck. I tried using the OnHotspotSelect event but maybe I'm doing something wrong because when using it the double click check isn't working and that's why I put the code in an Update() function.



using UnityEngine;
using System.Collections;
using AC;

public class DoubleClickEvent2 : MonoBehaviour
{
    void Update()
    {
        if (AC.KickStarter.playerInput.GetMouseState() == MouseState.DoubleClick)
        {
            if (KickStarter.playerInteraction.GetActiveHotspot().tag == "NavHotspot")
            {
                KickStarter.playerInteraction.GetActiveHotspot().RunUseInteraction();
            }
        }
    }
}

Comments

  • The OnHotspotSelect event couldn't be used exclusively, because that is run the moment a Hotspot becomes selected - not continuously while one is active.

    You haven't said which line is causing the NullReferenceException, but I agree that it's likely because of the inactive Hotspot.  Null-checking for it should fix the problem:

    using UnityEngine;
    using System.Collections;
    using AC;

    public class DoubleClickEvent2 : MonoBehaviour
    {
        void Update()
        {
            if (AC.KickStarter.playerInput.GetMouseState() == MouseState.DoubleClick)
            {
                Hotspot activeHotspot = KickStarter.playerInteraction.GetActiveHotspot ();
                if (activeHotspot != null && activeHotspot.tag == "NavHotspot")
                {
                    KickStarter.playerInteraction.GetActiveHotspot().RunUseInteraction();
                }
            }
        }
    }

  • Yes, the error was caused by the line checking the tag name of the hotspot. Sorry I forgot to mention it. The Null-cheking addition solved the issue.

    I feel a bit stupid now because I tried using a null check but I used it on the tag instead which I now see why it couldn't work :P

    Thanks for the help and sorry for wasting your time on such a trivial thing.
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.