i have been searching for days trying to solve this.
What I want is to detect all objects with specific tags within triger check, and then I want a guiTexture to appear, follow and scale accordingly to distance (follow and scale is done) so all I need now is for the guiTexture to appear AND follow the right object.
The detection works.
“targetArrow” is the name of the guiTexture (which got an attached script(UIFollowTarget).
sensorDetectionGUI.js
#pragma strict
var distanceToPlayer : float;
var graf : GUITexture;
var nameN : String;
var namnet : Transform;
function OnTriggerEnter(trigger : Collider){
Debug.Log("something");
if (trigger.tag != "Player"){
Debug.Log("not me");}
if (trigger.tag == 'HOSTILE')
{
Debug.Log("hostile");
//attempts to find and give name and transform
nameN = trigger.transform.name;
namnet = trigger.transform;
//find the script name n give it to variable
var script: UIFollowTarget = targetArrow.GetComponent(UIFollowTarget);
//do stuff to other script
script.targetName = nameN;
script.target = namnet;
//this one gives error
Debug.Log(nameN);
//i dont know what im doing
var pointer : GUITexture;
pointer = Instantiate(graf.guiTexture);
}
}
UIFollowTarget.js
var target: Transform; //found in other script
var targetName : String; //found in other script
var color : Color;
function Update()
{
// place the GUITexture at the targets position:
transform.position = Camera.main.WorldToViewportPoint(target.position);
if (target.gameObject.tag == 'HOSTILE')
{
//give color
guiTexture.color = Color.red;
}
//distance
distanceToPlayer = Vector3.Distance(target.transform.position, Camera.main.transform.position);
//scale to distance
guiTexture.pixelInset.width = 100-distanceToPlayer;