How to make a trigger area change a canvas' text?
So i'm fairly new to Unity, and i'm looking for a script that can make a trigger area change some text i have on a canvas. For instance, the player is walking down a hallway, and at the end of the hallway is a canvas with text that says come closer, and when you pass through the trigger area in the hallway the text changes to go away. Please assume i have no experience using C sharp or Javascript. Thank you very much. :)
Answer by UrielKane · Jun 30, 2016 at 04:03 AM
import UnityEngine.UI;
// U need to import UnityEngineUI in order to acces some of this class.
// change import to using on C#.
// you have to put this script on the same object that have the trigger.
public var MyUitext : Text;
function OnTriggerEnter (other : Collider) {
if(other.gameObject.tag == "Player"){
MyUitext.text = "Im in the Trigger"
}
}
function OnTriggerExit (other : Collider) {
if(other.gameObject.tag == "Player"){
MyUitext.text = "Im out of the Trigger"
}
}
This code is not tested but it should work. If you want to change the what text is displayed just change whatever is in the "". If you prefer you can declare two public variables of type String and do somthing like (MyUitext.text = Mystring). This way you will be able to change the text depending on the gameobject instance that is using this script. This means you can have 2 o 3 diferent gameobject with diferent text to dysplay when enter or exit the trigger.
Hope it help! @DaMasterGames
Your answer
Follow this Question
Related Questions
Weird text field bug? 1 Answer
Trigger FinishLine 1 Answer
Get single component from canvas, instead of all components 1 Answer
Unity 2D sorting item by color 0 Answers
TextGenerator with Canvas Scaler 0 Answers