- Home /
How to connect RayCast and GUI Text?
var hit : RaycastHit;
function Update() {
if (Physics.Raycast (transform.position, transform.forward, hit, 10) && hit.collider.gameObject.CompareTag("Player")){
print ("detected!");
guiText.text = "Pick Up";
}
else {
print ("nothing..");
}
}
How can I make my guiText become a string ("Pick Up" in my case) when its detected by the RayCast?
Comment
yes I am. The RayCast works fine, but I don't know how to connect them (Im just a beginner in scripting)
Best Answer
Answer by Benproductions1 · Feb 20, 2013 at 03:12 AM
Simple, you just have to have a variable for weather or not the text should display
var ShowText:boolean = false;
//First lets check for the raycasting
function Update() {
if (/*Physics.Raycast stuff here*/) {
ShowText = true;
}
else {
ShowText = false;
}
}
//Now let's display the GUI
function OnGUI() {
if (ShowText) {
GUILayout.Label("This is some Text"); //This is a GUI function that displays text automatically on the screen
}
}
Hope this clears things up, Benproductions1