Display issues with GUI only showing details for last object in list.

I have some code to display some info on the screen depending on what the player is looking at, the code is below and only shows text for the last Item, in this case the Enemy, it seems to ignore the tree, the debug code shows Im looking at the tree but the UI is never updated.

If I switch “Tree” for “Enemy” then “Tree” will display on the UI but not “Enemy”.

Any Ideas why this is happening whould be very appreciated, or if there is a better way of doing this would love to know, I expect there to be more entity types in the game and I dont think lots of if statements are a good idea.

Thank you

Kind Regards


void TargetIdentify()
        //Find the exact hit position using a raycast
        Ray ray = fpsCam.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0)); //Just a ray through the middle of your current view
        RaycastHit hit;  
        //check if ray hits something
        //Vector3 targetPoint;
        if (Physics.Raycast(ray, out hit))
           // targetPoint = hit.point;
            if (hit.collider.gameObject.CompareTag("Tree"))
                Debug.Log("Tree targeted");
                iD.color = Color.green;
            if (hit.collider.gameObject.CompareTag("Enemy"))
                Debug.Log("Enemy targeted");
                iD.color = Color.red;
            iD.color = Color.cyan;

You’re using “info.SetText()”, which is overwriting the previous value of the info text.

If you want both pieces of text to appear, you’ll need to do string addition, such as "info.text += “Enemy”.

The best thing would be for you to build your string throughout the TargetIdentify method, then just make the one call to SetText at the end.

Anyone have any more ideas how to get this to do what i want?