Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

How to print player transform to log?

Discussion in 'Scripting' started by Hereisme6, Jan 16, 2018.

  1. Hereisme6

    Hereisme6

    Joined:
    Jan 3, 2018
    Posts:
    4
    Hello, i was wondering how i would print player transform to log with text? like for example:
    Code (CSharp):
    1. Debug.LogFormat("Player Position:",transform.position.ToString);//doesn't work for some reason
    2.         print(transform.position.y);//works
    3.         print(transform.position.z);//works
    4.  
    and also how would i stop the log to be written like this: http://prntscr.com/i134pu
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You can do this:
    Code (csharp):
    1. print("Player position: " + transform.position);
    2. // or
    3. print("Player position: " + transform.position.ToString());  // notice ToString is a method "()".
    4.  
    Those 2 options will print them all on one line.

    Updating in 1 place is not an option, afaik. Sorry. If you meant adjusting the same line.

    And for clarity, the Transform is the class/component. The 'position' is a struct belonging to it. :)
     
    Hereisme6 likes this.
  3. Hereisme6

    Hereisme6

    Joined:
    Jan 3, 2018
    Posts:
    4
    Yeah so it works perfectly but it does look stupid when it prints the "Player Position" text with it many times.. : http://prntscr.com/i13cnn
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Indeed. If it's being printed very frequently, I personally try to keep those to a minimum and for only certain tests. Infrequent, I don't mind either way, while debugging; if the info is helpful in some way, that's all I need.
    Just remove it when it's no longer needed.
     
  5. Hereisme6

    Hereisme6

    Joined:
    Jan 3, 2018
    Posts:
    4
    How would i make it so that it opens when i click GUI button? i have this debug menu here, which would be great idea to to use to print it: https://prnt.sc/i13euu And also how would i open and close GUI box with its contents from a button of a keyboard like the tilde key? Sorry if it goes a bit out of topic, next time i make new topics if i have more questions.
     
  6. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    Something like this?
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ExampleClass : MonoBehaviour {
    5.     private bool isDrawingDebugMenu = false;
    6.  
    7.     // When enter is pressed, invert the boo, True
    8.     // becomes false and vice verse
    9.     private void Update(){
    10.         if(Input.GetKeyDown(KeyCode.Enter))
    11.             isDrawingDebugMenu = !isDrawingDebugMenu;
    12.     }
    13.  
    14.     private void OnGUI(){
    15.         // if isDrawingDebugMenu is false, return early so the menu isnt drawn
    16.         if(isDrawingDebugMenu == false)
    17.             return;
    18.  
    19.         // Menu code down here
    20.     }
    21.  
    22. }
    Hand written so may have a syntax error somewhere.

    Or, if its a GUI button which hides the menu I think it would be:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ExampleClass : MonoBehaviour {
    5.     private bool isDrawingDebugMenu = false;
    6.  
    7.     // When enter is pressed, invert the boo, True
    8.     // becomes false and vice verse
    9.     private void Update(){
    10.         if(Input.GetKeyDown(KeyCode.Enter))
    11.             isDrawingDebugMenu = true;
    12.     }
    13.  
    14.     private void OnGUI(){
    15.         // is isDrawingDebugMenu is false, return early so the menu isnt drawn
    16.         if(isDrawingDebugMenu == false)
    17.             return;
    18.  
    19.         if(GUI.Button(/* Some Params */))
    20.             isDrawingDebugMenu = false;
    21.  
    22.         // Menu code down here
    23.     }
    24.  
    25. }
     
    Hereisme6 likes this.
  7. Hereisme6

    Hereisme6

    Joined:
    Jan 3, 2018
    Posts:
    4
    Alright, thanks that worked very well. Would you know how to change the exact button text color to like yellow when its enabled not like this: http://prntscr.com/i16v81 where it turns all buttons and texts to the same color.

    And also how would i make it so that the game pauses when menu is opened and stays paused until menu is closed and prints some text box to screen saying game is paused or something.
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    If that is for in-game, I'd suggest that you use UGUI. You can find these/this by the 'Create' menu -> UI -> (panel , text, etc..). Much more widely used, and talked about on the forums.
    My knowledge of OnGUI stuff is limited, but you can search the manual.

    One way to pause the game is to set the timeScale to zero. If you do that, remember to set it back to '1' when you unpause. Adding text to the screen can be done with the UGUI mentioned above and creating a 'Text' game object :)
     
  9. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    If you really wanna go with that legacy UI system, you would cache the current color, set a new color, draw the tinted elements and set the old color again before the next UI elements that need to be rendered using the old color.

    Code (csharp):
    1. //semi-pseudo code
    2. DrawSomeNormalElements(); // renders with default color
    3. previousColor = GUI.color; // cache the current color
    4. GUI.color = yourTintColor; // set the new color, everything that follows will now be colored
    5. DrawColoredElements();   // some UI elements
    6. GUI.color = previousColor; // set the color you cached previously
    7. DrawSomeOtherNormalElements(); // render some more UI elements using the old color
    There are also gui styles and skins for the legacy system, but seriously, if you want to implement not just plain, simple, quick and temporary UI, you're better off using the new system. It's much more convenient, easier to setup and configure.

    Pausing has already been mentioned, just a little side note: everything that shouldn't be paused must not be dependant on that time scale (either not time dependant at all or it needs to rely on the unscaled time values). There should actually be quite alot of examples out there. Just grab one and play with it just a little bit.