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!