{"id":3495,"date":"2015-03-01T19:35:36","date_gmt":"2015-03-01T18:35:36","guid":{"rendered":"http:\/\/t-machine.org\/?p=3495"},"modified":"2015-03-01T19:35:36","modified_gmt":"2015-03-01T18:35:36","slug":"unity3d-display-a-variable-instantly-on-screen","status":"publish","type":"post","link":"http:\/\/new.t-machine.org\/index.php\/2015\/03\/01\/unity3d-display-a-variable-instantly-on-screen\/","title":{"rendered":"Unity3D: display a variable instantly on screen"},"content":{"rendered":"<p>You need this ALL THE TIME when developing, and Unity lacks it as a core feature:<\/p>\n<ol>\n<li>Create a Text label on your GUI\n<li>Drag\/drop a variable onto the label, and it will automatically display the value\n<\/ol>\n<p>Completely impossible, due to short-sighted design decisions in the Unity Editor from 10 years ago that never got changed. So, let&#8217;s fix it. It&#8217;s hugely annoying not being able to do this!<\/p>\n<p><!--more--><\/p>\n<h3>Approach<\/h3>\n<p>Using Reflection, we&#8217;ll look at the target object, find the list of all properties and fields, and find the one with the desired name, and display it.<\/p>\n<h3>Source<\/h3>\n<p><span style=\"font-size: large; color: #0A0\">Here&#8217;s the original source; but if you want to improve it, <a href=\"https:\/\/gist.github.com\/adamgit\/42253d7bb9320bbb49bf\">click here for a Gist you can fork, comment on,<\/a> etc<\/span><\/p>\n<p>[csharp]<br \/>\nusing UnityEngine;<br \/>\nusing System.Collections;<br \/>\nusing UnityEngine.UI;<br \/>\nusing System.Reflection;<\/p>\n<p>\/**<br \/>\nLicense: do whatever you want.<br \/>\nAuthor: @t_machine_org<br \/>\n*\/<br \/>\npublic class DisplayVariable : MonoBehaviour<br \/>\n{<br \/>\n\/** 1\/2: Drag\/drop a gameobject that has a var you want to display&#8230; *\/<br \/>\n\t\tpublic GameObject target;<br \/>\n\/** 2\/2: Type the name of the variable that you want to display&#8230; *\/<br \/>\n\t\tpublic string variableName;<\/p>\n<p>\/** Don&#8217;t refresh at 60FPS; wasteful! *\/<br \/>\n\t\tprivate float updateNSeconds = 0.25f;<br \/>\n\t\tprivate float lastUpdateTime = 0f;<\/p>\n<p>\t\t\/\/ Update is called once per frame<br \/>\n\t\tvoid Update ()<br \/>\n\t\t{<br \/>\n\t\t\t\tlastUpdateTime += Time.deltaTime;<\/p>\n<p>\t\t\/** Don&#8217;t refresh at 60FPS; wasteful! *\/<br \/>\n\t\t\t\tif (lastUpdateTime &gt; updateNSeconds) {<br \/>\n\t\t\t\t\t\tlastUpdateTime = 0;<\/p>\n<p>\t\t\t\t\t\tText myText = GetComponent&lt;Text&gt; ();<br \/>\n\t\t\t\t\t\tif (myText == null) {<br \/>\n\t\t\t\t\t\t\t\tDebug.LogError (&quot;Missing Text object, please disable this DisplayVariable component&quot;);<br \/>\n\t\t\t\t\t\t} else if (variableName == null || target == null) {<br \/>\n\t\t\t\t\t\t\t\tmyText.text = &quot;[Unattached]&quot;;<br \/>\n\t\t\t\t\t\t} else {<\/p>\n<p>\t\t\t\t\t\t\t\tbool foundIt = false;<br \/>\n\t\t\t\t\t\t\t\tstring valueAsString = &quot;&quot;;<\/p>\n<p>\t\t\t\t\t\t\t\tforeach (Component subComponent in target.GetComponents ( typeof(Component))) {<br \/>\n\t\t\t\t\t\t\t\t\t\tFieldInfo[] fields = subComponent.GetType ().GetFields ();<br \/>\n\t\t\t\t\t\t\t\t\t\tforeach (FieldInfo fieldInfo in fields) {<br \/>\n\t\t\t\t\t\t\t\t\t\t\t\t\/\/Debug.Log(&quot;Obj: &quot; + subComponent.name + &quot;, Field: &quot; + fieldInfo.Name);<br \/>\n\t\t\t\t\t\t\t\t\t\t\t\tif (fieldInfo.Name.Equals (variableName)) {<br \/>\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tvalueAsString = fieldInfo.GetValue (subComponent).ToString ();<br \/>\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tfoundIt = true;<br \/>\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tbreak;<br \/>\n\t\t\t\t\t\t\t\t\t\t\t\t}<\/p>\n<p>\t\t\t\t\t\t\t\t\t\t\t\tif (foundIt)<br \/>\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tbreak;<br \/>\n\t\t\t\t\t\t\t\t\t\t}<\/p>\n<p>\t\t\t\t\t\t\t\t\t\tif (! foundIt) {<br \/>\n\t\t\t\t\t\t\t\t\t\t\t\tPropertyInfo[] properties = subComponent.GetType ().GetProperties ();<br \/>\n\t\t\t\t\t\t\t\t\t\t\t\tforeach (PropertyInfo propertyInfo in properties) {<br \/>\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\/\/Debug.Log (&quot;Obj: &quot; + subComponent.name + &quot;, Property: &quot; + propertyInfo.Name);<br \/>\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tif (propertyInfo.Name.Equals (variableName)) {<br \/>\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tvalueAsString = propertyInfo .GetValue (subComponent, null).ToString ();<br \/>\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tfoundIt = true;<br \/>\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tbreak;<br \/>\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t}<\/p>\n<p>\t\t\t\t\t\t\t\t\t\t\t\t\t\tif (foundIt)<br \/>\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tbreak;<br \/>\n\t\t\t\t\t\t\t\t\t\t\t\t}<br \/>\n\t\t\t\t\t\t\t\t\t\t}<br \/>\n\t\t\t\t\t\t\t\t}<\/p>\n<p>\t\t\t\t\t\t\t\t\/** set the text *\/<br \/>\n\t\t\t\tmyText.text = variableName+&quot; = &quot;+valueAsString;<br \/>\n\t\t\t\t\t\t}<br \/>\n\t\t\t\t}<br \/>\n\t\t}<br \/>\n}<\/p>\n<p>[\/csharp]<\/p>\n<h3>Usage<\/h3>\n<ol>\n<li>Add this script to any GameObject that has a Text component (e.g. create from menu &#8220;UI &#8211; Text&#8221;)\n<li>Drag\/drop the object you want to debug onto the &#8220;Target&#8221; field\n<li>Type the C# source-code name of the variable\/field you want to debug into &#8220;Variable Name&#8221; field\n<\/ol>\n<p>&#8230;and it will update (by default: 4FPS. Tweak as needed)<\/p>\n<h3>Improvements<\/h3>\n<ul>\n<li>It would be much much cooler to have a drop-down menu for the variable name.\n<li>It would be neater to save the FieldInfo stuff at Editor time \/ startup, and stop using Reflection multiple times per frame.\n<li>But this is a debug tool, so frankly: neither of those are essential.\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>You need this ALL THE TIME when developing, and Unity lacks it as a core feature: Create a Text label on your GUI Drag\/drop a variable onto the label, and it will automatically display the value Completely impossible, due to short-sighted design decisions in the Unity Editor from 10 years ago that never got changed. [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[67,69],"tags":[],"_links":{"self":[{"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/posts\/3495"}],"collection":[{"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/comments?post=3495"}],"version-history":[{"count":6,"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/posts\/3495\/revisions"}],"predecessor-version":[{"id":3501,"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/posts\/3495\/revisions\/3501"}],"wp:attachment":[{"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/media?parent=3495"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/categories?post=3495"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/tags?post=3495"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}