How can I get a stack trace printed to the console log? In my breakout game something is calling Destroy on every brick as soon as the level loads and I can’t figure out what’s doing that.
Debug.Log will print out a stack trace.
Really? If that’s the case there there must not be any stack because I’ve never seen that even once, and I’ve peppered my code with Debug.Log statements already.
Oh hmm. I just checked and it looks like the stack trace doesn’t show up in the main console window but rather in a separate pane. I think I might have that minimized at home so that i’ve never seen it. I’ll have to check that out tonight.
I hope it traces into the unity engine itself and not just my own code.
Well, no it looks like it just really isn’t printing a stack trace. At least, not a useful one. When I log from the brick OnDestroy method I just get this:
brick destroy
UnityEngine.Debug:Log(Object)
Brick:OnDestroy() (at Assets/Scripts/Brick.cs:12)
That just tells me where I called Debug.Log. It doesn’t tell me what is calling Destroy on my object.
I’m not sure a full stack trace of OnDestroy would help you anyway.
OnDestroy is called on objects at the end of the frame, not when Destroy is called. So a stack trace wouldn’t actually lead you to the Destroy call. To top it off the Unity messages are called via some weird internal reflection from the C++ side of the engine.
A better method might be to search your code for Destroy, and throw the Debug.Log in there.
Yeah, that was the first thing I did. I’ve even tried commenting out all lines that could destroy the bricks. They are still getting destroyed. Hmmm I have a thought. Here is the code that creates the bricks
public void start_brick_breaker() {
Debug.Log( "start brick breaker" );
game_mode = GameMode.BrickBreaker;
SceneManager.LoadScene( "level_1" );
base_setup();
Instantiate( brick_block );
}
This is called from the GameController (a DontDestroyOnLoad object) in the “main” scene when I click the button to start the game. It calls LoadScene to load the new leveland then creates the block of bricks. Could the bricks be getting created in the “main” scene instead of “level_1”? I had assumed that LoadScene was synchronous, but if the actual scene load doesn’t happen until the end of the frame or the start of the next frame, then I bet I’m creating the bricks in main, and they are immediately getting destroyed when main is unloaded and the new scene loads up.
Gotta go try that.
Yup. The problem was the objects were being created in the wrong scene. I has bin edumacated.