Forum rules - please read before posting.

Find all Hotspots by Tag (or name eventually)

edited April 2020 in Technical Q&A

Hi Chris,
I am trying to create a kind of grouping for Hotspots, so that I can enable/disable Hotspots in a scene accordingly.

My first thought was using Unity Tags and get all GameObjects with the same tag by using FindGameObjectsWithTag
But then I am stuck on converting GameObject to Hotspot so that I can trigger the TurnOn() command.
Tried

foreach (GameObject myObj in GameObject.FindGameObjectsWithTag(myTag)){hotspot = myObj;}

but got the following error: "Cannot implicitly convert type 'UnityEngine.GameObject' to 'AC.Hotspot' ".

Maybe I am missing something, but is this even possible?

Second try was to put in the Hotspot name a kind of prefix and use the name as a reference, but I couldn't find any function that finds all Hotspots by Name in the Scripting Guide.

Any help would be much appreciated.
Thanks a lot.

Comments

  • Try something like:

            foreach (GameObject myObj in GameObject.FindGameObjectsWithTag(myTag)) 
            { 
                Hotspot hotspot = myObj.GetComponent<Hotspot>();
                if (hotspot) { hotspot.TurnOn(); }
            }
    

    that should do the trick

  • Thanks a lot @HarryOminous, works like a charm!

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.