- Home /
Bug hunting in the code...
is there a way to put a watch on a variable or array to find out what process is writing to it?
At present i have a "database" (a list of a couple of thousand items) that should be getting used as a reference ONLY, never written to, once it's set up. i then have a second list (the inventory) that is getting read from and written to often. But for some reason when i update an item in the inventory it updates the refernce database and it shouldn't do this. I want to track down which line of code is doing the "write to the database" command. I have looked though ALL the code on the project and can't find anything that looks to be causing this behavior! anyone got any ideas? please?
Answer by Tehnique · Jul 23, 2014 at 06:15 AM
For a variable, you can make accessors for it (get/set), put a breakpoint in the setter, and look at the callstack when the breakpoint is hit.
You could create a property for getting the variable(s) and ensure all calls now reference this, then trace the stack to find each calling function within the get / set accessor.
previous:
List<items> databaseVariables;
modify to
List<items> _databaseVariables;
List<items> databaseVariables {
get { return _databaseVariables; }
set { _databaseVariables = value; }
}
This should hopefully make no changes in your code (you'll need to name the variables correctly but here's how the accessors should look).
Then change the get accessor to do additional requirements
using System.Diagnostics;
using System.Reflection;
get {
StackTract st = new StackTrace();
$$anonymous$$ethodBase mb = st.GetFrame(1).Get$$anonymous$$ethod();
Debug.Log("Get Accessor, Calling function - " + mb.Name);
Debug.Break();
return _databaseVariables;
}
Hopefully this will give you a little insight into using Diagnostics, Reflection and Accessors to help some debugging.
thanks i'll have to have a look at how to do that! any links to ttorials on how to do that?
scrub that, found it (http://unity3d.com/learn/tutorials/modules/intermediate/scripting/properties) VERY helpful thank you so much! :)
Your answer
