Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Problem hiding the enemy gui

Discussion in 'Scripting' started by foxter888, Jun 4, 2011.

  1. foxter888

    foxter888

    Joined:
    May 3, 2010
    Posts:
    530
    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;

    }




    }
     
  2. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    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. ;)
     
  3. RingOfStorms

    RingOfStorms

    Joined:
    Oct 23, 2010
    Posts:
    584
    • In case you don't know how to do the code tags here is an example:

      Code (csharp):
      1.  [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]​