Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Custom editor tool causes crash

Discussion in 'Scripting' started by johanhenriksson, Aug 18, 2014.

  1. johanhenriksson

    johanhenriksson

    Joined:
    Jun 24, 2014
    Posts:
    4
    I've encountered a very strange problem that I can't seem to find a solution too. However I suspect that it's an issue related to serialization.

    I have an A-star based pathing system, and in order to quickly create pathing graphs I've written a simple tool to connect nodes. The nodes are basically just game objects with a simple script that contains a list of references to other node objects. My editor tool takes all the nodes (called Cookies) in the current selection and attempts to connect them to eachother. To save the changes it makes, I'm calling SetDirty on the node object after modifying its edges.

    The thing is, that it seems to work just fine. And in fact, it works just fine on my machine (running OSX), but some scenes just crash the unity editor when they are loaded by my teammates working on windows machines. I haven't got a crash log atm, but from what I could tell there was not really any information about what went wrong.

    If I load up the corrupted scenes (on osx) and remove all the node objects, the other guys can load the scene again. Crash occurs when opening the scene in the editor or attempting to load it using LoadLevelAsync.

    Code (CSharp):
    1.  
    2.     public class NodeTool
    3.     {
    4.         [MenuItem("Pathing/Connect Nodes %g")]
    5.         public static void InterconnectNodes()
    6.         {
    7.             var cookies = new List<Cookie>();
    8.             foreach(var obj in Selection.gameObjects) {
    9.                 var cookie = obj.GetComponentInChildren<Cookie>();
    10.                 if (cookie != null)
    11.                     cookies.Add(cookie);
    12.             }
    13.  
    14.             Debug.Log(string.Format("Connecting {0} cookies", cookies.Count));
    15.  
    16.             foreach(var cookie in cookies) {
    17.                 foreach(var other in cookies) {
    18.                     if (cookie == other)
    19.                         continue;
    20.                     cookie.Connect(other);
    21.                 }
    22.                 EditorUtility.SetDirty(cookie);
    23.             }
    24.         }
    25.     }
    26.  
    And the actual cookie class:
    Code (CSharp):
    1.  
    2.     [Serializable]
    3.     public class Cookie : MonoBehaviour
    4.     {
    5.         [SerializeField]
    6.         public List<Cookie> Edges = new List<Cookie>();
    7.  
    8.         public void Connect(Cookie other) {
    9.             if (!Edges.Contains(other))
    10.                 Edges.Add(other);
    11.         }
    12.     }
    13.  
    (I added the serialization attributes recently, but they don't seem to make any difference what so ever)
     
  2. johanhenriksson

    johanhenriksson

    Joined:
    Jun 24, 2014
    Posts:
    4
    Shameless bump :)

    I've read something about the serializer perhaps inlining objects and then deserializing multiple instances instead of one referenced object, but in my case this shouldn't happen right? Since all the references are MonoBehaviours owned by the scene, it should properly serialize them as references?
     
  3. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    They are saved by reference. [Serialize] ad [SerializedField] are both redundant, since MonoBehaviour are always serialized, and the same thing for public fields.

    Frankly, without the crash log, there's no evidence towards this snippet being the culprit.
     
  4. johanhenriksson

    johanhenriksson

    Joined:
    Jun 24, 2014
    Posts:
    4
    Thanks for your reply. I kind of figured that the serialization attributes were reduntant, I just put them there to make sure :)

    I'm pretty sure the problem is related to this code since I can "un-corrupt" scenes by removing all the cookie objects. I'm guessing that the problem is related to serialization since the problem appears the first time you reload the scene.

    Anyways, I've got a crash log (not sure if it's the right one though).
     
  5. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    That's a Window crash report, try getting the Unity's crash report. On Windows, it should be in "%localappdata%\Unity\Editor\"

    Note that only 2 editor log are kept, the current one, and the previous one.
     
  6. johanhenriksson

    johanhenriksson

    Joined:
    Jun 24, 2014
    Posts:
    4
    Ah yeah. This is the actual editor crash log :)

    From what I can see in the stack trace it looks like some kind of mesh corruption? hmm

     
  7. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    At first sight, sure looks like it.