- Home /
[4.6 JS] How to show GUI text on a trigger enter and then take it away on exit?
I'm doing a kind of level choice scene (like Mario) and I want it to show GUI text (like the name of the level) when the player enters an invisible trigger. Now, this would be easy but since 4.6 it is not as easy as just putting it in a GUI.Label or something (otherwise if you can still do that, please remind me how to then). I'm not really amazing at coding, and everything I have found has been really confusing and hasn't worked (e.g instantiate). Why isn't it so simple anymore? Please just help me show GUI text in a trigger!
I have to add this comment as you cannot have more than one answer per thread so
you can change the script so its attached to the player and it will read the name of what it collides with you can do that by adding these changes
using UnityEngine;
using System.Collections;
public class SomeScript : $$anonymous$$onoBehaviour {
//This script is now attached to the playerObject
//add this variable to store the level name
private string levelName = "";
private bool showName = false;
//Add this variable to the method
void OnTriggerEnter2D(Collider2D col)
{
levelName = col.gameObject.name;
showName = true;
}
void OnTriggerExit2D()
{
levelName = "";
showName =false;
}
void OnGUI()
{
//change the display to level name
if(showName)
GUI.Label(new Rect(10,10,100,100), levelName);
}
}
now you just make new objects and you player will find its name, but to get more information you would want to make a level class so you can have information like coins collect, and level times.
but for now this will work
Answer by Runalotski · Jul 23, 2015 at 03:17 PM
To use Triggers you would use unity's functions of
OnTriggerEnter() OnTriggerExit() OnTriggerStay()
to use this make sure one of the objects has a rigidbody2D (assuming its 2D colliders becaus said mario like) and to remove physics tick the IsKinematic (if you dont want them)
and make sure that there are boxColliders2D on each object with IsTrigger Selected
The Least cody looking way of doing it is to add this script to the level object that you wish to show its name, make a new c# script and call it SomeScript
using UnityEngine;
using System.Collections;
public class SomeScript : MonoBehaviour {
//this will tell us if the player
// has enter or has exit the collider
private bool showName = false;
//this is a unity function that fires
// when this 2dCollider enters another collider
void OnTriggerEnter2D()
{
//we know that the player has just enterd
// the collider so we want to show name
showName = true;
}
//this is unity function that fires when
// this collider exits another collider
void OnTriggerExit2D()
{
//we know the player has left the
// collider so we dont want to show name
showName =false;
}
//OnGUI is a unity function that will let us use GUI elements
void OnGUI()
{
//if showName is true then show the lable
if(showName)
{
//this creats a lable in top left corner and will
// display the name of the object from the hierarchy
GUI.Label(new Rect(10,10,100,100), transform.name);
}
}
}
now drop it onto your level object,
now when your char enters it will show the name of your level object at the top and when you leave it will go away.
this is the same code as above but with no comments
using UnityEngine;
using System.Collections;
public class SomeScript : MonoBehaviour {
private bool showName = false;
void OnTriggerEnter2D()
{
showName = true;
}
void OnTriggerExit2D()
{
showName =false;
}
void OnGUI()
{
if(showName)
GUI.Label(new Rect(10,10,100,100), transform.name);
}
}
Thanks, it is basically 2 dimensional but built with 3D, but thanks for the help. I was really confused with how to make the GUI text.
Do you know if there's a way I can use a script to do this for every level, ins$$anonymous$$d of making a new script for each one?
Your answer
![](https://koobas.hobune.stream/wayback/20220611232710im_/https://answers.unity.com/themes/thub/images/avi.jpg)