need help with Raycast and GUIText

Hello there people! I love this forum, I spend soooo much time reading it, but never post… Until now…

Ok here’s my problem, Im of course really new, to coding, and Im starting in JS. I have all ready finished one game, but I done most of it with triggers & colliders, now Im am trying to start a new project with Raycasting…

What I would like to accomplish is to look at an object (a palm tree…) and have a guitext show up. I managed to do it having a gui text on each object and enableing it on and off, but I know from my last game that this is not a very optimised way of doing this.

I’ve tried this (no laughing guy!:roll:

function Update () 
{

var fwd = transform.TransformDirection(Vector3.forward);
var hit : RaycastHit; 
Debug.DrawRay(transform.position, fwd * 10, Color.green);

if(Physics.Raycast(transform.position, fwd, hit, 10))
{

if(hit.collider.gameObject.name == "PalmCollider")
{
hit.collider.gameObject.guiText.text = "Palm Tree";
Debug.DrawRay(transform.position, fwd * 10, Color.blue);
} 
else
{
hit.collider.gameObject.guiText.text = "";
}
}
}

But it only shows the text of the 2nd object, what have I done wrong? I have tried some other solutions saving the guiText in a inspector variable, but no cigar.

many thanks in advance for any help regarding this issue!

Cheers guys! Keep it up!

I’ve just realised that I only get a response from the last item in the script, even if I put 4 objects, the text only works on the last one… This raycasting is really kinda tricky…

It’s probably working right, but since every hit you replace the GUIText text, it will only shows the last hitted item. In your case, better use RaycastAll:

public var text : GUIText;
 
function Update () 
{

 var fwd = transform.TransformDirection(Vector3.forward);
 var hit : RaycastHit; 
 var nameList : String;

 Debug.DrawRay(transform.position, fwd * 10, Color.green);
 nameList = "";
 for(hit in Physics.RaycastAll(transform.position, fwd, 10))
 {
  if(hit.collider.gameObject.name == "PalmCollider")
  {
   nameList = "Palm Tree" + "

" + nameList;
Debug.DrawRay(transform.position, fwd * 10, Color.blue);
}
}
text.text = nameList;
}

This way, when you put this script in a GameObject, It will have a field to put a GUIText Object. Just create one and drag it to the “text” field an test it :slight_smile:

thanks a lot!!! work a charm! Just for a quick mental note, what is the "
" for?

Thanks again, your a star! :wink:

sorry, but one last thing… ejem… xD.

What it be correct to use the code in this way:

#pragma strict

public var text : GUIText;

function Update () 
{

 var fwd = transform.TransformDirection(Vector3.forward);
 var hit : RaycastHit; 
 var nameList : String;

 Debug.DrawRay(transform.position, fwd * 10, Color.green);
 nameList = "";
 for(hit in Physics.RaycastAll(transform.position, fwd, 10))
 {
  if(hit.collider.gameObject.name == "PalmCollider")
  {
   nameList = "Palmera" + "

" + nameList;
Debug.DrawRay(transform.position, fwd * 10, Color.blue);
}
}

 for(hit in Physics.RaycastAll(transform.position, fwd, 10))
 {
  if(hit.collider.gameObject.name == "WoodenBox")
  {
   nameList = "Caja de Madera" + "

" + nameList;
Debug.DrawRay(transform.position, fwd * 10, Color.blue);
}
}

  for(hit in Physics.RaycastAll(transform.position, fwd, 10))
 {
  if(hit.collider.gameObject.name == "dingy")
  {
   nameList = "Bote Salvavidas" + "

" + nameList;
Debug.DrawRay(transform.position, fwd * 10, Color.blue);
}
}

 text.text = nameList;
}

I meen, repeating the action over and over again for each object? Or should I take a different aproach? thanks again astorga, you was very helpfull!