- Home /
GUI Update
I have a gui script here that displays the hull of a selected target from the selected targets pref file but the gui window does not update till I click off and back on to the target which does update the window…
Check this out… using UnityEngine; using System.Collections; using System.Collections.Generic;
public class Hud : MonoBehaviour
{
public GUISkin SensorHud;
//private string textstring = "Hello";
private float Shiphull;
void Update ()
{
var script = GameObject.Find("aaMain Camera").GetComponent<SelectTarget>();
Shiphull = script.Shiphull;
}
private void OnGUI()
{
GUI.skin = SensorHud;
//print the window based on center of screen, centering rects based on top corner, so divide screen w and h by 2
//and subtract half of the w and height
Rect windowSize = new Rect(Screen.width - 300,Screen.height - 250,300,250);
windowSize = GUI.Window(1, windowSize, MyWindow, "");
}
private void MyWindow(int id)
{
GUILayout.Label ("Sensor Analysis",GUILayout.Width(649));
GUILayout.BeginVertical();
var script = GameObject.Find("aaMain Camera").GetComponent<SelectTarget>();
if(script.gui == "yes")
{
GUILayout.Label ("Shipname: " + script.Shipname);
GUILayout.Label ("Shiphull: " + Shiphull);
}
if(script.gui == "no")
{
GUILayout.Label ("Shipname: N/A");
GUILayout.Label ("Shiphull: N/A");
}
//GUILayout.Space(8);
//GUI.TextField(new Rect(11,40,400,200),textstring);
GUILayout.EndVertical();
}
}
What's wrong here, shouldn't the window be updating every frame?
I don't know if it would help with your issue but I'd strongly recommend caching a "aa$$anonymous$$ain camera" reference outside of Update() and OnGUI() calls, for performance reasons. Either keep a private 'camera' variable and set it in Start() or make it public and set it via the Inspector.
I assume that the SelectTarget component can change, which is why you're querying for this each frame? How are you setting this on the camera? That could be where your problem is occurring.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class SelectTarget : $$anonymous$$onoBehaviour
{
public Transform selectedTarget;
public string Shipname;
public float Shiphull;
public bool Shields;
public string gui;
void Start()
{
gui = "no";
}
void Update()
{
if (Input.Get$$anonymous$$ouseButtonDown(0))// when button clicked...
{
gui = "no";
RaycastHit hit; // cast a ray from mouse pointer:
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// if enemy hit...
if (Physics.Raycast(ray, out hit))
{
DeselectTarget(); // deselect previous target (if any)...
selectedTarget = hit.transform; // set the new one...
selectTarget(); // and select it
}
}
}
private void selectTarget()
{
gui = "yes";
if(selectedTarget.CompareTag("[Enemy] Galaxy"))
{
var n = selectedTarget.GetComponent<Galaxy_Pref>();
Shipname = n.Shipname;
Shiphull = n.Shiphull;
Shields = n.Shields;
print("Galaxy");
}
if(selectedTarget.CompareTag("Intrepid"))
{
var n = selectedTarget.GetComponent<Intrepid_Pref>();
Shipname = n.Shipname;
Shiphull = n.Shiphull;
print("Intrepid_Pref");
}
}
private void DeselectTarget()
{
if (selectedTarget)// if any guy selected, deselect it
{
selectedTarget = null;
}
}
}
Answer by Tomer-Barkan · Nov 06, 2013 at 12:14 AM
In your SelectTarget
script, you only update the ShipHull
when a target it selected. Since your window gets its info from there, then it also only updates only when you select the target.
If you want it to update every frame, you also need to update it every frim in the SelectTarget
script.