I am looking to add a sort of hints box to the bottom of the screen, mostly for triggering, but I don’t think it is efficient to create 50+ of the things, is there any way to change the 4.6 Text UI code in script?
I work primarily in Javascript and would prefer to be able to understand it, so i’m not sure how useful C# will be to me, there are other things I will be adding to this.
#pragma strict
import UnityEngine.UI;
var textToUse = "Text";
var text : Text;
private var drawGUI = false;
function Start ()
{
text.text = "";
}
function Update ()
{
if (drawGUI == true)
{
text.text = "" + textToUse;
}
}
function OnTriggerEnter (theCollider : Collider)
{
if (theCollider.tag == "Player")
{
drawGUI = true;
}
}
function OnTriggerExit (theCollider : Collider)
{
if (theCollider.tag == "Player")
{
drawGUI = false;
text.text = "";
}
}
when you use variables you need to feed it into a string which is what “” is for. and you don’t need to use ongui any more. for what your wanting its best not to use “text = GetComponent(Text);” because if you use that you need to drop the script onto the ui text element so that it can get the text component if you do that the Ontrigger stuf wont work . when you dont youse “text = GetComponent(Text);” you can put the script anywhere and drop the text element onto the script in the inspector. your ontrigger stuff will work and all your scripts can youse one text element other wise you would need a lot of text elements on one canvas you wouldn’t be able to reuse it.
nah not for the new UI. OnGUI was used to generate things solely through script but now you access the elements through script via the canvas so its not needed. as long as your script knows what its accessing your fine.
sorry if my explanations don’t make sense I’m still learning as well.
that script is good because you can put it on 50+ objects and they will all use that one text element.
#pragma strict
import UnityEngine.UI;
var textToUse = "Text";
var text : Text;
function Start ()
{
text.text = "";
}
function OnTriggerEnter (theCollider : Collider)
{
if (theCollider.tag == "Player")
{
text.text = "" + textToUse;
}
}
function OnTriggerExit (theCollider : Collider)
{
if (theCollider.tag == "Player")
{
text.text = "";
}
}
now that I think about it its probably best not to use update to change the text component, probably doesn’t matter but for this you don’t actually need to use the update function because it can be done the same in Ontrigger enter.