- Home /
How to hide/unhide a GUI image when character enters trigger.
Hey! Im pretty new to Unity and currently making a 2D platformer game. I want a GUI image to appear when my character enters a trigger box, i have placed the image in the scene and hidden it and connected a trigger to it as it's child. Then I made a script and placed it on the trigger, the script looks like this.
#pragma strict
private var textboxshow: boolean = false;
var textbox: UnityEngine.UI.Image;
function OnTriggerEnter2D(Col: Collider)
{
if(Col.tag == "Player")
{
textboxshow = true;
} else {
textboxshow = false;
}
}
function OnGUI()
{
if (textboxshow == true)
{
GetComponent.<Renderer>().enabled = true;
}
}
And when i enter the trigger volume the image does not appear, what have I done wrong? All help greatly appreciated!
(Edit: Fixed Collider to Collider2D, still does not work however)
Answer by RealSoftGames · Oct 23, 2015 at 02:46 PM
Hello, do you have rigidbody on both collider and trigger. This is required even if you are not using physics, just set it to kinamatic. Also in your OnTriggerEnter. Add if(col.tag == ("Player") print("Player enter trigger zone")
Use that to check if you are entering the trigger, also make sure your player has the tag Player.
Also if you have the gameobject disabled, the trigger shouldnt work.
I did have a rigidbodies enabled on both and I managed to solve it like this changing the script, thanks for the help!
private var textboxshow: boolean = false;
var textbox : GameObject;
function OnTriggerEnter2D(Col: Collider2D) {
if(Col.tag == "Player") {
if (textboxshow == true) {
textboxshow = false;
textbox.SetActive(false);
} else {
textboxshow = true;
textbox.SetActive(true);
}
}
}
function OnTriggerExit2D(Col: Collider2D) {
if(Col.tag == "Player") {
if (textboxshow == true) {
textboxshow = false;
textbox.SetActive(false);
}
}
}
Your answer
Follow this Question
Related Questions
show gui when look certain object 2 Answers
Setting Scroll View Width GUILayout 1 Answer
Re-hiding a guiTexture 1 Answer
Show GUI on collision 2 Answers