- Home /
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 Onx
==
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");
info.SetText("Tree");
iD.color = Color.green;
}
if (hit.collider.gameObject.CompareTag("Enemy"))
{
Debug.Log("Enemy targeted");
info.SetText("Enemy");
iD.color = Color.red;
}
else
info.SetText("");
iD.color = Color.cyan;
}
}
Answer by Zaeran · Dec 18, 2020 at 06:23 PM
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.
It is only supposed to display, either 'enemy' or 'tree' and will ultimatly be a longer list, but it will only display one value. I want to make the player aware of what they are targeting at any time.
Answer by Onx · Dec 24, 2020 at 08:11 PM
Anyone have any more ideas how to get this to do what i want?