How to print player transform to log?

Hello, i was wondering how i would print player transform to log with text? like for example:

Debug.LogFormat("Player Position:",transform.position.ToString);//doesn't work for some reason
        print(transform.position.y);//works
        print(transform.position.z);//works

and also how would i stop the log to be written like this: Screenshot by Lightshot

You can do this:

print("Player position: " + transform.position);
// or
print("Player position: " + transform.position.ToString());  // notice ToString is a method "()".

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. :slight_smile:

1 Like

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

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.

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.

Something like this?

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    private bool isDrawingDebugMenu = false;

    // When enter is pressed, invert the boo, True
    // becomes false and vice verse
    private void Update(){
        if(Input.GetKeyDown(KeyCode.Enter))
            isDrawingDebugMenu = !isDrawingDebugMenu;
    }

    private void OnGUI(){
        // if isDrawingDebugMenu is false, return early so the menu isnt drawn
        if(isDrawingDebugMenu == false)
            return;

        // Menu code down here
    }
 
}

Hand written so may have a syntax error somewhere.

Or, if its a GUI button which hides the menu I think it would be:

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    private bool isDrawingDebugMenu = false;

    // When enter is pressed, invert the boo, True
    // becomes false and vice verse
    private void Update(){
        if(Input.GetKeyDown(KeyCode.Enter))
            isDrawingDebugMenu = true;
    }

    private void OnGUI(){
        // is isDrawingDebugMenu is false, return early so the menu isnt drawn
        if(isDrawingDebugMenu == false)
            return;

        if(GUI.Button(/* Some Params */))
            isDrawingDebugMenu = false;

        // Menu code down here
    }
 
}
1 Like

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.

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 :slight_smile:

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.

//semi-pseudo code
DrawSomeNormalElements(); // renders with default color
previousColor = GUI.color; // cache the current color
GUI.color = yourTintColor; // set the new color, everything that follows will now be colored
DrawColoredElements();   // some UI elements
GUI.color = previousColor; // set the color you cached previously
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.