Game Development Reference
In-Depth Information
With our
Alarms
in place, all we need now is a simple script to change the state of
the
Alarm
once the Hero triggers it. So create a new
C#
script called
AlarmScript
and replace its contents as follows:
using UnityEngine;
public class AlarmScript : MonoBehaviour {
void OnTriggerEnter(Collider other)
{
gameObject.SetActive(false);
}
}
Just a very basic script to deactivate the
Alarm
if the Hero's Sphere enters the alarm's
plane collider, nothing fancy.
To finish off, add the
AlarmScript
script to the
Alarm Prefab
and we are set.
Who watches the watchers?
To close, we need to replace the boilerplate in our
MyAlarmScannerModule
input
module that is managing the events to scan the alarms and send in the killer robots if
any foolish Hero tries to plunder the gold.
To keep with the basic example thread, we will just add another public property to
our custom input module that manages all the available alarms in the scene. We will
then iterate over those alarms and look for any that have been made inactive by the
clumsy Hero tripping over them.
If you want to add a more complicated system to discover objects to
monitor in a scene, be aware that this discovery has to happen
before
the input module is processed, for example, in a custom rayscanning or
detection module.
If you try to build an array of objects (say using the
GetObjectsWIthTag
method) in the
Process
method, it will be unable to interrogate the list.
It's not immediately obvious why this doesn't work, but if I was to hazard
a guess, it's that the processing of the Input modules is either handled on
another thread or is using some uber performant process. Simply put, it
won't be able to see changes in your scene.
I'll see about adding a more complicated scenario in the follow-up example
on my blog.