Search Unity

Script working correctly only after compiling [FIXED]

Discussion in 'Scripting' started by RickyX, Feb 3, 2016.

  1. RickyX

    RickyX

    Joined:
    Jan 16, 2013
    Posts:
    280
    What are the couses of this ? When i compile the script and run the game, it works correctly. But when i stop the game and play it again, it won't work the second time untill i recompile the script again. Very strange. I cannot show the scripts, i think it's not important, like, what could couse such a strange bug ? I'm working with instantiating UI elements and repositioning them, also using some foreach there and setting their parent and their variable references aswell, that's it. Thanks in advance.
     
  2. Immanuel-Scholz

    Immanuel-Scholz

    Joined:
    Jun 8, 2013
    Posts:
    221
    Well, good luck with your bug then. I can only provide some general hints in this case..
    • Start deleting stuff from your script. Maybe suddenly the problem vanishes. Then inspect what you deleted last
    • If that doesn't work: Create a new project and start copying stuff from your current one over. Test whether the behaviour is there after each copy. Once you got the problem reproduced in a new project, think about what you copied last.
     
    Laperen likes this.
  3. RickyX

    RickyX

    Joined:
    Jan 16, 2013
    Posts:
    280
    I understand and thank you. But when you think about it, code shouldn't be able to do such a thing, right ? Like, how can a piece of code couse the game to work only after compiling, and then second time you press play it won't work.... Then just add anything to the code like a comment or anything at all, recompile, it'll work again the first time but second it won't... Seems rediculous, like i have no idea where to go from here, i will try what you said but i really don't think it will help.
     
  4. Immanuel-Scholz

    Immanuel-Scholz

    Joined:
    Jun 8, 2013
    Posts:
    221
    You could do all sorts of strange things.. the assembly reload that occurs after recompile is not the same as if you switch to play mode.

    For an easy example [DidReloadScripts] does not get called when switching to play mode. Or EditorApplication.playmodeStateChanged is not triggered when compiling...

    There are also a handful of Unity bugs when it comes to calling order of functions like OnEnable.. these trigger differently when doing an compile and switching to play mode.. [ExecuteInEditMode] might also play a role...

    Without the script under question, its just poking in the fog..
     
    Mycroft likes this.
  5. RickyX

    RickyX

    Joined:
    Jan 16, 2013
    Posts:
    280
    The reason why i hate sharing the code is becouse when i finish my project, someone will find this post and be like: hey that code looks like it would be from this game, and it would be unprofessional. But, damn, this bug sucks. Here:

    Code (csharp):
    1.  
    2. Hidden, fixed
    EDIT: also, this piece of code is in a method, and the method is called at Start()
    EDIT: oh i'm silly i didn't even explain what happens. So basically, when i run the first time, this clone spawns and repositions as i told it to do. But the second time, everything works correctly except the positioning, the clone is not in the place it's supposed to be at, where i told it to be.
    EDIT: but yeah you might need all of the reference codes but i really can't share them, it would be too much


    FIXED: Okay i've got it. The problem was, you cannot get a transform position from a wanted object if it's inactive, only if it's active. For some wierd reason, after first compile, it have me the position but second times it doesen't wanna give a position of an inactive transform, so yeah.
     
    Last edited: Feb 4, 2016
  6. Immanuel-Scholz

    Immanuel-Scholz

    Joined:
    Jun 8, 2013
    Posts:
    221
    Interesting point of view...

    Well.. good luck in your career then. We probably won't meet again, but I wish you all the joy and hapiness of game development you can find.. :)