x


System.Reflection calls crashing Unity

Having a bit of an oddball issue here. I'm attempting to dump a scene to XML using Reflection and Unity crashes/is forced to close.

I'm fairly certain I've pinpointed the issue to a single line:

PropetyInfo.GetValue()
I get the property (prop) from a list of properties generated from a Component (comp) and then I make this call:
prop.GetValue(comp, (object[])null);

When the code hits that line, Unity freezes up and I get the lovely "UnityEditor has stopped working" popup. Sometimes, however, the code will work to a point (there's a debug for this: At Text) and then stop. If the script is run again, then the crash happens.

EDIT: Thanks to Ricardo Arango for trying it out and showing that it worked with simple objects and scripts. Now the problem is that it doesn't work with some scripts, the one that definitely doesn't work is AnB's UIButton. This also happens to be the home of that Text property that I mentioned above.
Here's the code:

 public static void WriteFromProject()  {  //setup the settings and path  XmlWriterSettings xmlSettings = new XmlWriterSettings();  xmlSettings.Indent = true;  string path = "xml/test.xml";  //create the writer  using(XmlWriter writer = XmlWriter.Create(path, xmlSettings))  {  //write the header  writer.WriteStartDocument();  //write the header  writer.WriteStartElement("ISE");  //grab all the objects  UnityEngine.Object[] objs = GameObject.FindObjectsOfType(typeof(GameObject));  foreach(GameObject obj in objs)  {  //open the tag for a gameobject  writer.WriteStartElement("GameObject");
         //set the attributes
         writer.WriteAttributeString("objType", obj.GetType().ToString());
         writer.WriteAttributeString("objName", obj.name);

         //for each component, push a tag
         Component[] comps = obj.GetComponents(typeof(Component));

         //TODO: Do more for attaching scripts/components later
         foreach(Component comp in comps)
         {
          //get the type object
          Type compType = comp.GetType();

          //open the component tag
          writer.WriteStartElement("Component");
          //set the name as an attribute
          writer.WriteAttributeString("Name", compType.FullName);
          Debug.Log("Component Name: " + compType.FullName);
          //writer.WriteElementString("Component", compType);

          //open fields tag
          writer.WriteStartElement("Fields");

          //then, dump all the fields with it
          //do this for call base classes as well
          //TODO: Only grab public variables

          for(;compType != null; compType = compType.BaseType)
          {
              //make a subtree for every class/baseclass
              writer.WriteStartElement("Class");
              //have the class name for an attribute
              writer.WriteAttributeString("Name", compType.FullName);

              PropertyInfo[] properties = compType.GetProperties();
              Debug.Log("Field Length" + properties.Length + " " + "for " + compType.FullName);
              foreach(PropertyInfo prop in properties)
              {
                 //if we're not allowed to read or write this, move on
                 //reason being: 1) Right now we need to read it. 2) When read from xml, we'll need to write it too
                 if(!(prop.CanRead || prop.CanWrite))
                 {
                   continue;
                 }
                 if(prop.Name.CompareTo("Text") == 0)
                 {
                   Debug.Log("At Text");

                 }

                 prop.GetValue(comp, (object[])null);

              }

              //close the class subtree
              writer.WriteEndElement();
          }




          //close fields tag
          writer.WriteEndElement();
          //close component tag
          writer.WriteEndElement();

         }
         //close the gameobject element
         writer.WriteEndElement();
       }
       //write end of doc
       writer.WriteEndDocument();
    }
}

Any thoughts?

more ▼

asked Jun 09 '11 at 03:24 PM

Cloud779 gravatar image

Cloud779
16 3 3 5

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

That works fine for me in a simple scene (camera + cube). Try debugging the code to see what the values of the variables are.

more ▼

answered Jun 09 '11 at 03:46 PM

Ricardo Arango gravatar image

Ricardo Arango
688 11 13 27

(comments are locked)
10|3000 characters needed characters left

Hey! You're right! If I take out an object that was using some middleware script (AnB's UIButton), then it works just fine.

Now looking into why it crashes with that script attached...

more ▼

answered Jun 09 '11 at 04:15 PM

Cloud779 gravatar image

Cloud779
16 3 3 5

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x617
x263
x148
x38

asked: Jun 09 '11 at 03:24 PM

Seen: 1013 times

Last Updated: Jun 09 '11 at 04:27 PM