Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Carbongrip · Nov 05, 2013 at 10:38 PM · guiupdatedamagewindowhud

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?

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tomer-Barkan · Nov 05, 2013 at 11:31 PM 1
Share

It should. Please share your SelectTarget script.

avatar image Huacanacha · Nov 05, 2013 at 11:34 PM 1
Share

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.

avatar image Carbongrip · Nov 06, 2013 at 12:06 AM 0
Share
 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;
         }
       }
 }
 

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Add EditorUI elements when a button is pressed 0 Answers

Where can I find the scripts used in the Player State Machine & HUD/GUI Tutorials? 1 Answer

Health & HUD Script, not syncing 1 Answer

how will i get every time values in table window? 0 Answers

GUI HUD Problem 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges