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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by SinisterRainbow · Sep 29, 2013 at 05:56 AM · debugdebuggingconsoleunity input

Unity Debug Console - I want to input things during Runtime

I'd like to turn on/off debugging features in Unity during runtime to test for different features. Have I just been google incompetent or is there a way to do this? I'm assuming you cannot directly, so I've started writing a class where I can easily turn on and off specific debugging information (to test different parts of the game).. But, without being able to write to the console during runtime (such as in CryEngine) it only half serves it's purpose. Any useful feedback appreciated.

Edit: If you find the selected answer useful, please thumb-it-up, it really deserves it.

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 Fattie · Sep 29, 2013 at 09:22 AM 1
Share

Sure, add this to your code

 Debug.Log("On the unity answers site, it is essential to" +
         "TIC$$anonymous$$ questions that have been answered.");
 Debug.Log("This is the only way to keep the site tidy.");

note that you can also easily include almost any variable,

 Debug.Log("The value of enormousTank is " +enormousTank);

Also try Debug.LogError() for another effect

FInally look at the doco page for "Debug" for many other tips.

avatar image SinisterRainbow Fattie · Sep 29, 2013 at 11:36 AM 0
Share

I appreciate the response @Fattie. I think my question may have been a bit unclear perhaps. I was thinking more along the lines of hitting ~ tilde or something and bringing up a special console where I can type variable names and bool values to turn them on and off during runtime.

avatar image Fattie Fattie · Sep 29, 2013 at 12:23 PM 0
Share

Fair enough. If you want to do that it is entirely built-in to Unity. Hit play. Now look at the Inspector. Click on your Player, Enemy, etc. In the Inspector, you can change booleans, type in new values etc. as the game is running. So that's all done. Hope it helps.

1 Reply

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

Answer by Jamora · Sep 29, 2013 at 10:45 AM

You can do much better than writing to the console when working with Unity.

You can create your own debugger, which prints out whatever you've set it to print.

 using UnityEngine;
 using UnityEditor;
 using System.Collections;
 using System.Collections.Generic;
 
 public class DebuggerEditor : EditorWindow
 {
     enum bools{
         False,
         True
     }
     
     string newTag = "";
     bools newBools = bools.False;
     bool newValue = false;
     
     [MenuItem("MyDebugger/debug")]
     public static void Init(){
         GetWindow<DebuggerEditor>();
     }
     
     void OnEnable(){
         MyDebugger.tags.Add("TheImportantThings",true);
         MyDebugger.tags.Add("RandomThings",true);
     }
     
     void OnGUI(){
         foreach(KeyValuePair<string,bool> kvp in MyDebugger.IteratableDictionary()){
             GUILayout.BeginHorizontal();
             EditorGUI.BeginChangeCheck();
             newValue = GUILayout.Toggle(MyDebugger.tags[kvp.Key], kvp.Key);
             if(EditorGUI.EndChangeCheck()){
                 MyDebugger.SetTag(kvp.Key,newValue);
             }
             GUILayout.EndHorizontal();
         }
         
         GUILayout.BeginHorizontal();
         newTag = GUILayout.TextField(newTag);
         newBools = (bools)EditorGUILayout.EnumPopup(newBools);
         GUILayout.EndHorizontal();
         if(GUILayout.Button("Add"))
             MyDebugger.SetTag(newTag,bool.Parse(newValue.ToString()));
     }
 }

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public static class MyDebugger {
 
 #if UNITY_EDITOR
     public static Dictionary<string,bool> tags = new Dictionary<string,bool>();
 #endif
     
     public static void Log(string tag, string message){
 #if UNITY_EDITOR
         if(!tags.ContainsKey(tag)){
             Debug.LogWarning("Tag "+ tag +" not found in MyDebugger's TagDictionary! Adding it...");
             tags.Add(tag,true);
         }
         if(tags[tag] == true)
             Debug.Log(message);
 #endif 
     }
     
     public static Dictionary<string,bool> IteratableDictionary(){
 #if UNITY_EDITOR
         return new Dictionary<string, bool>(tags);
 #endif
     }
     
     public static void SetTag(string tag, bool flag){
 #if UNITY_EDITOR
         if(tags.ContainsKey(tag))
             tags[tag]= flag;
         else
             tags.Add(tag,flag);
 #endif
     }
     
 }

Now, you can control what gets debugged at any given moment in runtime from the EditorWindow. You just call MyDebugger.Log(tag, message). If the tag doesn't exist, you'll get a warning and it'll get added automatically, though the idea is you know beforehand what will get debugged and add it to the OnEnable in the EditorWindow.

You can use MyDebugger in normal game code, as it will not cause any performance drop because of useless debugs when built because of the preprocessor directives.

A dictionary is not the best choice here, as they do not get serialized by Unity. If you need the serialization functionality, then you'd be better off with e.g. two lists instead.

Comment
Add comment · Show 2 · 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
avatar image SinisterRainbow · Sep 29, 2013 at 11:32 AM 0
Share

Excellent. This is everything I was looking for and the code to boot - much appreciated. I hope many get great use of it.

avatar image Fattie · Sep 29, 2013 at 12:24 PM 0
Share

Also an excellent Editor Program$$anonymous$$g Example.

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

17 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 avatar image

Related Questions

Double clicking console output does not take me to the corresponding line in code. 1 Answer

Is there any way to view the console in a build? 7 Answers

Input Button is not setup Error 1 Answer

How to use Webgl Debug Symbols? 0 Answers

How to clear a specific error/warning in the console output window? 0 Answers


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