Search Unity

Make simulated compiler error - pevent playmode

Discussion in 'Scripting' started by MaZy, Oct 18, 2019.

  1. MaZy

    MaZy

    Joined:
    Jun 29, 2012
    Posts:
    105
    Hi, if you have a compiler error (syntax error) in your code the syntax error will prevent you to enter playermode and do not allow you to build a game. Errors will stay permantly.

    I am looking the same way to simulate this via script. I want to make custom attribute which will force you to add a object via drag and drop. But I didn't found any idea to make this.

    I know I can make it via InitializeOnLoad and EditorApplication.update but this will just spam errors. I want to show once and make it not able to clear (via the clear button in the console)

    Any ideas?
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
  3. Debug.LogError or Debug.LogException aren't good enough?
     
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    You've got a chicken-and-egg problem. You can't drag-and-drop stuff around in the Unity inspector until after the code has compiled, which means you can't know at compile-time what stuff has been placed where.
     
  5. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Try EditorApplication.playModeStateChanged.

    Subscribe from within a static method that is marked with the InitializeOnLoadMethodAttribute. Then, check whatever needs to be checked, do your setup automatically or just log information / instructions, or open a setup window... Whatever you like.
     
  6. MaZy

    MaZy

    Joined:
    Jun 29, 2012
    Posts:
    105
    Thanks for the answers.

    No, thats why I started this thread, lol.

    I see what you mean but thats why I wrote "simulated compile error". So I could control myself and simulate when I need it. So you know if I use an attribute above a field and after the c# files gets compiled in the editor then you could throw an error and prevent from going into the playmode if you try. Already wrote also an auto-getcomponent. I never need to make getcomponent in awake anymore. It is automatically done via attribute and property drawer. Infact the next step what I wanted to try was to auto assign components without opening the scene. I am not sure if this is possible but I will see. But priority is very low for that at the moment. If I can touch the serialize field so it should possible.


    Yea, that is almost same way how I do at the moment. Didn't found any other possibility. But I took update, because you can press "clear" in the console and will not see the error again. When it starts to exit the EditMode I stop the playmode which is ok because it doesn't load the scene. However I get sometimes ui bugs. On some elements I have still the color of the playmode. I don't know why this happens (maybe I exit too fast?)

    The only last idea what I have is to make show an error if you try to exit editmode and stop the playmode before it starts.
    I will also look into the InitializeOnLoadMethodAttribute. maybe I could it combine with my custom attribute.
     
  7. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Ah, so you were hoping that Unity has a built-in feature specifically designed to do this?
     
  8. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    That's what I was trying to get you at. Not sure how you ran into that color glitch though...

    Anyways, if you plan to let other people use it, don't work with errors in the console. Especially, don't spam them once every update.

    What should do instead is open a window or navigate to a window and guide/instruct the user in order to get your components set up correctly. Or provide a setup button that auto runs a setup for the current scene.