Game Development Reference
In-Depth Information
Tips and Tricks
Knowing where to put spaces and new lines can be a point of concern
for many beginning scripters. In some cases, Unity is very tolerant of
spaces (for instance in the preceding code there could be spaces here
guiTextObject = GameObject or not (guiTextObject=GameObject) and
both would work. However, when describing a “phrase” (like GameObject
.Find(“GUITextHints”)) or using dot syntax, leave the spaces out.
Step 20: Create the trigger functionality with the following code:
private var guiTextObject : GameObject;
function Awake(){
guiTextObject = GameObject.Find(“GUITextHints”);
}
function OnTriggerEnter (other:Collider){
if (name == “Trigger-FindDoorHint”){
guiTextObject.GetComponent(EntryWayGUITextScript)
.FindDoorHint();
}
if (name == “Trigger-WaterHint”){
guiTextObject.GetComponent(EntryWayGUITextScript)
.StayOutOfWaterHint();
}
if (name == “Trigger-EMPHint”){
guiTextObject.GetComponent(EntryWayGUITextScript)
.TryEMPHint();
}
}
Why?
So here we get to the meat of triggers. The function OnTriggerEnter
(other:Collider) line is simply saying, “when another collider enters
the volume of the trigger, do the following.” What follows is an
extension of techniques we learned in the last chapter; when a
collider enters the trigger, it first checks to see if the name of the
object this script is attached to is Trigger-FindDoorHint. If it is,
it goes to the guiTextObject GameObject (which is our GUITextHints),
gets the component EntryWayGUITextScript (the script we wrote
earlier in this chapter), and fires the function FindDoorHint. Pretty
cool eh?
If the trigger this script is attached to is not named Trigger-FindDoorHint
it moves on to check for the next name on the list and so on.
The exciting idea here is one script on one object is going out finding
another object, finding a script on that object, and executing a function
within that other script.
Search WWH ::




Custom Search