ok i have a targetting script in which i can click on the enemy, select it and have him targeted to be able to kill him. now on my script once i target the enemy i can get a reference of his name mesh renderer and have it display his name on game. my problem is that i can't hide it because once i deselect him, he is no longer targeted and it can't get a reference of the mesh to be able to turn it off. my question would be anyway that can someone show me how i can store the last target and have his name be able to be turned off by the time he is deselected?? here's my scritp below. ******************************************************************* using UnityEngine; using System.Collections; public class Target2 : MonoBehaviour { public Transform target; private Transform _tempTarget; private Transform _myTransform; // Use this for initialization void Start () { target = null; } // Update is called once per frame void Update () { ClickOnTarget(); } private void ClickOnTarget() { RaycastHit hit; Ray ray = camera.ScreenPointToRay (Input.mousePosition); Transform selected = GameObject.FindWithTag("Enemy").transform; if(Input.GetMouseButton(0)) { if(Physics.Raycast (ray,out hit, 100.0f)) { if(hit.collider.gameObject.transform) { target = hit.collider.gameObject.transform; SelectTarget(); } else { target = null; } } } } private void TargetSelected(Transform tr) { target = tr; } private void SelectTarget() { Transform name = target.FindChild("Name"); if(name == null) { return; } name.GetComponent<MeshRenderer>().enabled = true; } private void DeselectTarget() { GameObject en = GameObject.FindWithTag("Enemy"); MeshRenderer el = en.GetComponent<MeshRenderer>(); el.enabled = false; //name.GetComponent<MeshRenderer>().enabled = false; target = null; } }
There are two ways to do this: Create an isSelected variable, if it is true, then display the text, if it is not, then don't. You can easily send a Message to change that state as you see fit. Change your way of thinking and have a GameController handle what is selected, and what is displayed on the screen. This can also be used to handle all the player screen controls and such. (Where it makes sense) Oh, and please use code tags when posting code.
In case you don't know how to do the code tags here is an example: Code (csharp): [color=blue][center][size=8]Your code here[/size][/center][/color] This is the way it looks when your typing it: [code] Your code here [/code]