Search Unity

Unusual issue with editor script?

Discussion in 'Editor & General Support' started by numberkruncher, Jul 22, 2012.

  1. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    I have a custom `EditorWindow` which has some controls for editing some properties of a custom `MonoBehaviour` (attached to game object prefab). The editor window can be used to edit a number of prefabs. For the most part this is working great, except occasionally an unusual problem is occurring.

    Sometimes a number of "broken" components are materializing with the exact same properties as the `MonoBehaviour` that is edited; except with the caption "MonoScript (None)". When looking in the inspector these components usually have the `EditorGUILayoutGUI.InspectorTitlebar` style caption, sometimes they have a much thicker caption (approx 32px in height).

    These "broken" components can be removed relatively easily and there is no data loss on the proper `MonoBehaviour`.

    Here is the simplified process that my editor window is doing:
    Code (csharp):
    1. selectedPrefab = EditorGUILayout.ObjectField("Edit Prefab", selectedPrefab, typeof(GameObject), false);
    2. SomeBehaviour some = selectedPrefab.GetComponent<SomeBehaviour>();
    3.  
    4. EditorGUI.BeginChangeCheck();
    5.  
    6. // Change a property directly
    7. some.age = EditorGUILayout.IntField("Age", some.age);
    8.  
    9. if (EditorGUI.EndChangeCheck()) {
    10.     EditorUtility.SetDirty(some);
    11.     // Not sure if this is necessary, playing it safe!
    12.     EditorUtility.SetDirty(selectedPrefab);
    13. }
    I have not been able to reproduce the error directly, but it does intermittently occur.

    Is the above code correct? Any ideas on what might be causing this?
     
  2. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    Okay it has happened again but I have a little more info:

    An extra component has been added which has the same data as the proper component, except the inspector title bar reads "{tick} (Script) {help} {gear}" (with described icons). The "Script" property reads "Type mismatch".

    Upon selecting "Save Project", exiting Unity and returning the new broken script is captioned "Mono Behaviour" with Script "None (MonoScript)"
     
  3. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    pic:
     

    Attached Files:

    • $pic.jpg
      $pic.jpg
      File size:
      72.4 KB
      Views:
      1,133
  4. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    This is purely a guess, but the issue seems to occur when Unity reloads/rebuilds scripts and DLLs...
     
  5. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    If you are creating managed dll's, make sure that every class that inherits (recursively) MonoBehaviour has a class outside of the dll! I had this issue as well exactly because of that. You get especially trouble if you are working with prefabs.
     
  6. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
  7. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Yes, that is wrong! It took me days to find out, why I can create everything perfectly with my development version of the Cloud System. But with the dlls it only worked when I created the clouds (which automatically creates prefabs). Then I could save the scene and when I opened it again, I got all those "Mono Behaviour" and nothing was working.
    I was aware that you could not have classes inheriting from Editor in dlls, so I made an experiment and moved all my classes that inherit from MonoBehaviour out of the dll and it worked.
    It is not needed to have the code outside of the dll. You can have a class ClassAInDll which inherits from MonoBehaviour with all the actual code compiled to the dll. But outside of the dll, you need another class A which inherts from ClassAInDll.
    The Unity developers are heavily working on the prefabs as they are prepering nested prefabs...

    Do you want to report that documentation bug, or should I do it?
     
  8. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    I am already committed to the DLL approach now.

    This is an implementation bug because the documented functionality appears to be broken.
     
  9. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    You are certainly right. It is a Unity bug and has nothing to do with the documentation.
     
  10. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    I will post a bug report with reference to this forum posting. Thanks for your help!
     
  11. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    Posted the bug report :)

    Interesting, have Unity released any information on this? I would be very interested to find out more.
     
  12. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    Also, regarding this bug. Are you compiling your DLL's with MonoDevelop or Visual Studio? Which runtime are you targeting?

    I do my development with Visual Studio but test and release the final extension with MonoDevelop with Mono/.NET 4
     
  13. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    It was once mentioned as a side note. Possibly in some release notes. I remember that I was surprised because it was not really a hot topic in this forum...
     
  14. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    I am using the compiler that is bundled with Unity, such that it should be as compatible as possible. Though I am using MonoDevelop as IDE, I compile everything myself. I have implemented a "simple" editor extension. I only did that because I am too stupid to understand how to setup the MonoDevelop project for that :)
     
  15. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    Here's the strange thing. Just for the sake of experiment I decided to go back to using MonoDevelop for all compiling during development, and this problem seems to have stopped... I am still using VC to edit my source code (just so much more pleasant!)

    It could be that I am just not doing the **thing** (whatever it might be) that triggers this problem to occur, or perhaps this is indeed the cause of the issue. The other interesting note is that the issue only ever applied to prefabs. Game objects in scenes were never effected, but all prefabs were.

    I will write about any further developments here, but I think this may be mystery solved...
     
  16. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    So the issue is now gone? No more MonoBehaviour issue if the script is in a dll? How did you compile in MonoDevelop?
    I think you are going to answer those questions as you are making more progress. Just wanted to show that there are others who are interested in this :)
     
  17. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    To compile my class library in MonoDevelop:
    • Open MonoDevelop and create a solution, add library projects to solution. (Instead of adding to the auto-generated solution, avoid the grief!)
    • I set the .NET compliance to 4.0 using project settings for both DLLs
    • Right-click solution and select build. I found that it is often useful to clean projects before building occassionally.
    • EXTRA STEP: Create a .bat file for windows and an automator application for osx which copies DLLs into the Assets directory. Invoke this as a custom post-build step.

    This is some advice that I took from a forum entry here somewhere, but sadly I was unable to find it the second time around. So I give credit for the first 3 steps to whomever originally provided it! The 4th step was an addition on my part though (so I can have a little credit too! :p)
     
  18. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    That's basically what I am doing with my custom compilation scripts. I don't understand why the prefab issue is resolved like that. I'll need to check my compilation arguments, maybe it's the 4.0 settings.
     
  19. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    Please let me know how this goes for you! If you have a reproducible case would you be able to send it to me so that I can investigate a little?