- Home /
Trigger Script Not Firing
I'm going through the book "Creating Games w/ Unity & Maya" by Adam Watkins. I've been stuck on the same section for 2 hours, and I can't figure out what is going wrong! The final trigger (LoadLevel) works, but the guiText triggers don't. I've pasted both JScripts below...
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( );
}
if (name == "Trigger-HallwayPortal"){
Application.LoadLevel ("Scene-Hallway");
}
}
and also
function FindDoorHint (){
guiText.enabled = true;
guiText.text = "Find the secure entrance to the base.";
yield WaitForSeconds (3);
guiText.enabled = false;
}
function StayOutOfWaterHint (){
guiText.enabled = true;
guiText.text = "Stay out of the water! At this temperature, it's lethal.";
yield WaitForSeconds (5);
guiText.enabled = false;
}
function TryEMPHint (){
guiText.enabled = true;
guiText.text = "Try using EMP to disable exterior secuirty system.";
yield WaitForSeconds (5);
guiText.enabled = false;
}
HELP!
Answer by asafsitner · Jul 17, 2012 at 08:02 AM
Wait, so the script with the OnTriggerEnter
method is located on the triggers?
Otherwise if it's part of the player object then you're comparing the player's name (`name` with lowercase returns the name this script is attached to) against what should be the triggers's name.
If it's on the player try it with other.name
instead to compare the trigger's name.
if it's on the triggers themselves... shouldn't you know what the trigger's name is?
In that case you can just remove that long list of if
s and just do what that specific trigger is meant to do. Also, double-check your names.
Answer by paverson · Jul 17, 2012 at 02:42 PM
thanks for the quick reply, asafsitner.
the script is indeed attached to the trigger (a big invisible cube that the FPSController goes through). the trigger's name is "Trigger-FindDoorHint" (hence the name == "Trigger-FindDoorHint").
I've tripled - no - quintuple checked my names, and even found a digital copy of the book online and copied and pasted the code! I'm about ready to give up, as I simply can't figure out what the heck is wrong (especially since the .LoadLevel command works). I've attached screengrabs, to help you visualize what I'm trying to do.
Answer by Meltdown · Jul 17, 2012 at 02:48 PM
Change
if (name == "Trigger-FindDoorHint"){
guiTextObject.GetComponent(EntryWayGUITextScript).FindDoorHint();
}
to
if (other.name == "Trigger-FindDoorHint"){
guiTextObject.GetComponent(EntryWayGUITextScript).FindDoorHint();
}
So you change the name check to other.name Do that for each one..
I tried that, and unfortunately no dice (again, the name isn't the issue, as it is firing on the last "Trigger-HallwayPortal" Trigger. I simply can't figure this damn thing out.
Have you tried using Debug.Log(other.name); or Debug.Log(name); in OnTriggerEnter to see what is actually happening and what trigger your character is colliding with?
I'd suggest also addding OnTriggerEnter to your character controller and Debug.Log(other.name); on that to see if your character is actually colliding with the trigger.
$$anonymous$$eltdown,
Thanks for the quick reply. I'm such a nOOb, that I don't even know what you're talking about (concerning Debug.Log(name). I'll try and search around for how to execute that and see if it works.
thanks.
In Unity press CTRL + SHIFT + C to open the Console window. You use this window to see errors and debug information. Each time OnTriggerEnter is called, you can use Debug.Log(variable); to see what the value is for any variable or string.. its very useful for working out problems.
I might be making some progress w/ this, thanks to you. I'm getting this feedback from the Console: $$anonymous$$issingComponentException: There is no 'GUIText' attached to the "Trigger-FindDoorHint" game object, but a script is trying to access it. You probably need to add a GUIText to the game object "Trigger-FindDoorHint". Or your script needs to check if the component is attached before using it.
The problem is that the GUIText IS attached to the game object (well, the two scripts from above are attached to it, which is what I'm assu$$anonymous$$g Unity is talking about). This is such a stumper, and I'm SURE it is some simple step I missed (even though I've gone through the book's instructions 3 times now!)
Answer by paverson · Jul 18, 2012 at 07:43 AM
Well, I guess it pays to be persistent (and a bit creative?). I figured - why make this so complicated with two scripts, and pare it down to one? I know the author was trying to show an efficient way that scripting could be applied across the level, but it just wasn't working for me. So (thanks to a lot of advice/help from you guys), I FINALLY figured it out. Now the different text prompts come up when I walk the FPSController through the various "invisible boxes". Well, that's roughly 5 hours down the tube. I have a LOT of appreciation/respect for those of you that know what you're doing when you're writing and reading this stuff! Thanks again! See below!
var guiTextDoor : GameObject;
var guiTextWater : GameObject;
var guiTextEMP : GameObject;
function Awake (){
guiTextDoor = GameObject.Find("GUITextHints");
guiTextWater = GameObject.Find("GUITextHints");
guiTextEMP = GameObject.Find("GUITextHints");
}
function OnTriggerEnter (other:Collider){
if (name == "Trigger-FindDoorHint"){
guiTextDoor.guiText.enabled = true;
guiTextDoor.guiText.text = "Find the secure entrance to the base.";
yield WaitForSeconds (3);
guiTextDoor.guiText.enabled = false;
}
if (name == "Trigger-WaterHint"){
guiTextWater.guiText.enabled = true;
guiTextWater.guiText.text = "Stay out of the water! At this temperature, it's lethal.";
yield WaitForSeconds (3);
guiTextWater.guiText.enabled = false;
}
if (name == "Trigger-EMPHint"){
guiTextEMP.guiText.enabled = true;
guiTextEMP.guiText.text = "Try using EMP to disable exterior secuirty system.";
yield WaitForSeconds (3);
guiTextEMP.guiText.enabled = false;
}
if (name == "Trigger-HallwayPortal"){
Application.LoadLevel ("Scene-Hallway");
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613074258im_/https://answers.unity.com/themes/thub/images/avi.jpg)