Search Unity

Bug Code and game is working completly different just few hours ago

Discussion in 'Editor & General Support' started by the_camalardo_16, Oct 18, 2020.

  1. the_camalardo_16

    the_camalardo_16

    Joined:
    Oct 18, 2020
    Posts:
    1
    Today I wake up and continue my game. Yesterday everything was normal. But when I reopen the project and Run the game, just for fun, the game fails because the audio don't starts. That was a little problem, that I "repair" in few minutes. That was running well yesterday, but today just didn't work. I had to change the way how I get an array of AudioSource, because it appears that it just don't load in the variable, to return it in a getter, so I tried to find all game objects with tag in the getter, then it worked. I don't take too importance to that, so I continued. But after hours there were more and more bugs like that. That was as if the start method was not working. No one were loading nothing to the variables in Start().
    There are some things that is working and others aren't.
    The new bugs came when I made a new scene to load an object that originally was in the main scene.
    Then I had to do the same method with everything. Use the call in the getter, because the variables in Start() does not load or it load but the getter don't give the object out of the script, except if I use a call. Something like

    private static Transform trans;

    void Start()
    {
    transform = GameObject.FindWithTag("thing");
    }

    public static Transform GetTrans()=>trans;

    This work well yesterday. Then everything stops to work and work with this:

    private static Transform trans;

    public static Transform GetTrans()=>GameObject.FindWithTag("thing");

    Obviously I don't want to use it. But I don't know why Unity change it o.o
    I updated unity, but it doesn't work... Help!
     

    Attached Files:

  2. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    497
    Code (csharp):
    1. private static Transform trans;
    2.  
    3. void Start()
    4. {
    5. transform = GameObject.FindWithTag("thing");
    6. }
    7.  
    8. public static Transform GetTrans()=>trans;
    Your GetTrans method returns the value of the private variable trans, yet you never set it. Instead you are trying to set transform in the Start method. Perhaps your code should be this:
    Code (csharp):
    1. private static Transform trans;
    2.  
    3. void Start()
    4. {
    5. trans = GameObject.FindWithTag("thing");
    6. }
    7.  
    8. public static Transform GetTrans()=>trans;
     
  3. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    Assigning to the transform property of a monobehaviour isn't even possible, it doesn't have a setter. Your original code (that supposedly "work well yesterday") can't even compile, yet alone run.

    There are 2 possibilities that come to mind:
    * that the code you posted in your OP isn't actually the same as the code that was working earlier.
    * that your code was never recompiled after you wrote the code that assigns to the "transform" property. The next time you launched Unity, it recompiled the code and failed to function.