How can I trigger GUI Text using a Box Collider?

I’ve attached a Box Collider to an empty GameObject, then dragged a script that I’ve written as follows:

function OnTriggerEnter()
{
function OnGUI ()
{
GUILayout.Label(“texthere”);
}

}

It’s not working though. Any suggestions? I feel I’ve made a very elementary mistake somehow…

Did you mark one of them as the “Trigger”? Those trigger events won’t get called for non-trigger enabled objects. (only enable the trigger on one, not both)

Hmm, what do you mean by one of them? I’m only using one Box Collider for this (although I have several Box Colliders in the scene, but they are for loading levels).

I’ve ticked the “Is Trigger” box and input the code, but I’m getting what seems like a syntax error - the debugger says I’m missing a semicolon. But when I change the highlighted error, more appear.

I’m thinking I may have written the script wrongly in the first place. It’s rather simple and I am a script newbie, so yeah…

Yeah, the combinations that cause events seem a bit complicated to grasp.

See chart at bottom here: Unity - Manual: Box collider component reference

Non-trigger collision events seem to work only with other non-trigger collision events. As soon as a trigger is flagged, the “trigger” events get called instead. I’m still not 100% sure why the difference is necessary … anyone? :wink:

I think it needs to be the other way around:

function OnGUI ()
{
     function OnTriggerEnter()
     {
          GUILayout.Label("texthere");
     }
}

Oh yeah, and to trigger that collider, the object that enters it needs to have a collider or a rigid body attached to it.

I tried what you suggested, but it doesn’t seem to be working (reversing the order of the code).

I’m not sure what you mean by the object entering the collider needing a collider/rigidbody attached.

This is because I am using a FirstPersonController and I have not encountered any difficulty when entering a Box Collider which has code to load a next level. Hence I feel that there is something wrong with the code itself. Still, your help is appreciated! :slight_smile:

Any advice from scripting pros? Hehe

You can’t have the OnTriggerEnter function inside OnGUI. You can get the result you want by having a boolean variable that is set in OnTriggerEnter and tested in OnGUI:-

var triggered: boolean;

function OnTriggerEnter() {
    triggered = true;
}


function OnGUI() {
    if (triggered) {
        GUI.Label(...);
    }
}

hi there.

I was trying to follow this thread to solve a similar problem in my game.

But it seems I am doing something wrong eventhough my code doesnt return any errors.

Im still very new to scripting in UNITY. I have so far only used C# and Javascript for web dev.

This is my code which I think is correct. (But obviously it aint since it wont work for me)

using UnityEngine;
using System.Collections;
using System;
using System.Text;
using System.IO;

public class DialoqueStartScript : MonoBehaviour {
	
	bool triggered;
		
	void OnTriggerEnter( Collider other)
			{
				if (other.gameObject.tag == "Player")
					{
						triggered = true;
					}
			}
			
	void OnGui()
		{
			if (triggered) //The dialogue starts.
				{
					GUI.Label(new Rect(300,300,500,200), "Hello World!");
				}
		}
		
	void OnTriggerExit( Collider other)
	{
		if (other.gameObject.tag == "Player")
		{
			//The dialogue ends.
		}
	}
}

I know the thread here mainly talks about javascript, but could anyone see what I am doing wrong? The GUI.Label aint never displayed when my player object enters the area I have made IsTrigger and my script attached to that.

Many thanks to those who can help :slight_smile:

1 Like

Are you definitely sure that the tag (not just the name) of the player is set to “Player”?

I am sure yes. Check my screenshot.
Player is tagged Player, and I have even tried to check Char under Player as Player also. No result.

267123--9599--$player_tag_656.jpg

The firstpersoncontroller has a rigidbody internally present.

to trigger a Trigger, one of the two objects involved in that “collision” must have a Rigidbody component attached beside its collider. if none of the two has one it would have the consequence that triggers won’t fire at all anymore.

as for the code itself:

move the functions out, you can’t not have function within function.
Both are standard callbacks and must be on the class public level. if you need to change a thing on the gui basing on the trigger, you must use a variable.
So you need to have

var labelText : String = "";
function OnGUI() {
 GUILayout.Label(labelText);
}
function OnTriggerEnter( other : Collider ){
 labelText = "...";
}

dreamora, that code worked for me, but it showed the top left hand corner of my screen is there a way to change that?

I had a much simplier solution but getting an error
NullReferenceException: Object reference not set to an instance of an object Showtext.OnTriggerEnter

usingUnityEngine;
usingSystem.Collections;
usingSystem;
usingSystem.Text;
usingSystem.IO;

publicclassShowtext : MonoBehaviour {
GUITextguiText;
//Usethisforinitialization
voidOnTriggerEnter(Colliderother) {
guiText.enabled = true;
}
voidOnTriggerExit(Colliderother) {
guiText.enabled = false;
}
}