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
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();
}
}
}
}
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.