- Home /
RaycastHit info works using print() but not GUIText?
I have a First Person Controller that you can use to walk around on top of a simple, large cube. On that Cube1 I have a smaller Cube2 and a Sphere1. Using this code, I was able to make a working raycast script:
var hit : RaycastHit;
function Update() {
if (Physics.Raycast (transform.position, transform.forward, hit, 10)) {
print("There is a " + hit.transform.name + " directly ahead!");
}
else print("There is nothing directly ahead.");
}
That worked fine, and spit out the object I was standing in front of in the print bar line (user dialogue?) at the bottom of the viewport in the Unity Editor. But when I created the game and ran it, that Print text didn't show up at the bottom of the game window. So I tried this in a new script:
var hit : RaycastHit;
function Update () {
if (Physics.Raycast (transform.position, transform.forward, hit, 10)) {
//GetComponent(GUIText).text = "There is a " + hit.transform.name.ToString() + " directly ahead!";
GetComponent(GUIText).text = "There is SOMETHING in front of you.";
}
else GetComponent(GUIText).text = "There is nothing in front of you.";
}
Here's where I'm confused. The line with the .ToString in it didn't work, so I commented it out and tried the line below it. That's when I realized it was always spitting out the else statement. It DOES change the GUIText's text component and shows "There is nothing in front of you." but I can't get the if statement to return as true. Obviously what I'm trying to do is get the screen to display that I'm looking at Cube2 and Sphere1 when I'm standing in front of them.
So my 2 questions are 1) What am I doing wrong or overlooking, and 2) Is there an easier way to do this? Thanks for the help in advance!
Answer by deltamish · Nov 03, 2012 at 11:59 AM
Hi, if dont want to set up a gui pos from script do as follows
Step 1 Create a gui text from Gameobject>other>GUI text place it where you like
add this code to your script
var texter:GUIText;
function Update () {
var hit : RaycastHit;
if (Physics.Raycast (transform.position, transform.forward, hit, 10)) {
texter.text = "There is" + hit.transform.name + " directly ahead!"
}
texter.text = "There is nothing in front of you.";
}
Your answer
Follow this Question
Related Questions
Raycast Hit Not Working (JS) 2 Answers
Raycast Coding Issues 1 Answer
RaycastHit always returns 0 1 Answer
C# raycast shooting script broken 1 Answer