Name Hovering over Weapon Model. Problem!

Hey guys,

I have a problem with a script. I’m trying to implement that the name of a weapon hovers over it. Here is the script:

    function OnGUI ()
{

for (var go : GameObject in FindObjectsOfType(GameObject)){
	for (var i=0;i<transform.childCount;i++){
 		if(go.tag==WeaponsArray *&& !go.transform.parent){*
  •  	if(Vector3.Distance(transform.position, go.transform.position)<20){*
    
  •  	GUI.Box(Rect(Camera.main.WorldToScreenPoint(go.transform.position).x,Screen.height-Camera.main.WorldToScreenPoint(go.transform.position).y,50,20), go.tag);*
    
  •  	}*
    
  •  }*
    
  • }*
    }

}
Btw: WeaponsArray is an array with all of the weapon names.
The script works in the way that the name does hover over the weapons. But the problem I have is that there are also names on the mirror side. I mean when I turn around there are names hovering on the positions where the weapons were when you would mirror them on the camera.
I searched for this problem and found something but it didn’t worked so I hope someone of you can help me.

You really shouldn’t use any Find function in OnGUI, as it is called several times per frame. About your problem, isn’t there a confusion between 2d and 3d ?

You have to do some kind of convertion from 3D coordinates to 2D point and that is mighty difficult. what you should do instead is put a 3D text object over each weapon and put this script over it to tell it to only appear if the player is a certain distance from it.

var player : Transform;
var minDist : float = 20;

function Update() {
    if(Vector3.Distance(player.position,transform.position)<minDist){
         GetComponent(TextMesh).active = true;
    }else{
         GetComponent(TextMesh).active = false;
    }    
}