Search Unity

What does this error mean?

Discussion in 'Scripting' started by cruising, Jul 5, 2015.

  1. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    329
    Hello!

    im getting this error, what does it mean?
    Code (CSharp):
    1. GetLocalizedString can only be called from the main thread.
    2. Constructors and field initializers will be executed from the loading thread when loading a scene.
    3. Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
    4.  
    It dont tell what script making the error, but its clearly my script for loading scenes with buttons.
     
  2. SomeGuy22

    SomeGuy22

    Joined:
    Jun 3, 2011
    Posts:
    722
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Often you get this when attempting to call a function at the definition stage. If you are doing this, try moving it to Awake instead.
     
  4. Shadamer

    Shadamer

    Joined:
    Sep 2, 2013
    Posts:
    1
    I got this error a couple of times after upgrading my project to Unity 5. Turns out I had trees I made from the tree creator in my scene. Once I removed them, the error went away.
     
  5. Hvost239

    Hvost239

    Joined:
    Oct 3, 2013
    Posts:
    2
    I have same solution
     
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    ???

    No official has commented on this thread. Do you have an official comment somewhere else?

    Normally the stance from Unity developers is to not change engine versions in late stage development.
     
    Mycroft likes this.
  7. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
  8. AshyB

    AshyB

    Joined:
    Aug 9, 2012
    Posts:
    191
    Old reply, but I just had this problem. For me its caused by trees. I grabbed everything in the scene, put it under an empty game object, made a prefab from it. Deleted the prefab. Errors gone.
     
  9. Todd-Wasson

    Todd-Wasson

    Joined:
    Aug 7, 2014
    Posts:
    1,079
    Same here. I've been making as much noise about it as I can for months and months without any reply from anyone at Unity. Is this ever going to be fixed?

    Code (csharp):
    1.  
    2. ArgumentException: GetLocalizedString can only be called from the main thread.
    3. Constructors and field initializers will be executed from the loading thread when loading a scene.
    4. Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
    5. TreeEditor.TreeGroupRoot..cctor ()
    6. Rethrow as TypeInitializationException: An exception was thrown by the type initializer for TreeEditor.TreeGroupRoot
    7.  
    I tried the prefab trick and it worked, but it'd be a lot better if this issue just never came up again.
     
    Last edited: Dec 15, 2015
  10. Todd-Wasson

    Todd-Wasson

    Joined:
    Aug 7, 2014
    Posts:
    1,079
    Actually no, it didn't work. The first time it kind of did (my original terrain was still there though, just unchecked) but the second time (deleting the original terrain) it caused a bunch of other things to break in addition to the original errors showing up again. Something to do with it being a prefab I guess.

    So... No. The prefab trick does not work.
     
    Last edited: Dec 15, 2015
  11. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    i think you were suppose to delete the prefab not the original :)
     
  12. Todd-Wasson

    Todd-Wasson

    Joined:
    Aug 7, 2014
    Posts:
    1,079
    Oh! :D I'll try that, thanks. :)
     
  13. Todd-Wasson

    Todd-Wasson

    Joined:
    Aug 7, 2014
    Posts:
    1,079
    Just tried it. It worked the first time I hit play, but after that the errors return, so nope, it didn't really work or I'm still missing something.

    It's a pain because any time I change scenes in play mode it pauses Unity so you can see the errors. When it's running in the Rift I turn off the mouse cursor which makes it a bit tricky to hit the play button.
     
  14. FlyAnvil

    FlyAnvil

    Joined:
    Aug 14, 2013
    Posts:
    9
  15. David-Berger

    David-Berger

    Unity Technologies

    Joined:
    Jul 16, 2014
    Posts:
    745
    Just to keep you updated, we have seen the problem and someone reported a very useful incident which we can use to reproduce the issue. We will work on a solution to the problem, in the meantime please try the workaround posted by @FlyAnvil and let us know in the thread of the workaround if it works for you or not as it might help to solve the issue when we know more about the behaviour. You can follow the report and outcome here: http://issuetracker.unity3d.com/iss...-error-messages-when-loading-scene-with-trees
     
    Mycroft likes this.
  16. ev3d

    ev3d

    Joined:
    Apr 19, 2013
    Posts:
    327
    FWIW, i was having the same issue...And this worked.. I deleted the trees from the scene and re-added them (luckily there position was not important).. and after doing that, the error is gone.

    Also, in case it matters, i went as far as deleting the trees and re adding them (useing the "edit trees" button in the terrain editor). I then repainted the trees on the terrain.
     
  17. daisySa

    daisySa

    Joined:
    Dec 29, 2011
    Posts:
    341
    @David Berger The solution suggested by FlyAnvil didn't work for me.

    I just selected Edit Tree and Apply for each tree in the terrain; I didn't try ev3d's solution of deleting and repainting each tree, as I have too many in the scene to do that all again unless absolutely necessary.
     
    Last edited: Jan 31, 2016
  18. Todd-Wasson

    Todd-Wasson

    Joined:
    Aug 7, 2014
    Posts:
    1,079
    Sorry, I'm missing something. Where are you clicking "edit tree" and "apply?" Are you clicking the tree prefabs in the project or is this something you're doing in the scene hierarchy?

    I'm using Terrain Composer which adds the trees to the terrain itself, so if this is something that has to be done directly on the trees in the Unity terrain editor, I'm not sure if I can solve the problem this way. If it's just something I can fix by fiddling with the tree prefabs directly, that'd be great because I only have a few of them in my scenes. Wouldn't take long at all to do.
     
  19. Todd-Wasson

    Todd-Wasson

    Joined:
    Aug 7, 2014
    Posts:
    1,079
    Think I found it. Guess it doesn't matter if I'm using Terrain Composer or not. The trees are there when I click the terrains in the hierarchy. However, when I click a tree and hit "edit trees" and then "edit tree" in the dropdown under that, I am not able to click "apply" on all of the trees. Some of them give the error "tree has already been selected as a prototype" as shown here. No idea what that means.




    edit tree.png
     
  20. Todd-Wasson

    Todd-Wasson

    Joined:
    Aug 7, 2014
    Posts:
    1,079
    Ok, I hit "apply" on all the trees that didn't give the error (some of those trees might not be in this scene, I seem to remember only using a few of them). It fixed it, but only for the first time that I pressed play. Once I stopped and ran it a second time, the errors returned.

    So no, this has the same results as the original fix. Still broken for me.
     
  21. daisySa

    daisySa

    Joined:
    Dec 29, 2011
    Posts:
    341
    I'm getting the same result - it only works the first time.
     
  22. David-Berger

    David-Berger

    Unity Technologies

    Joined:
    Jul 16, 2014
    Posts:
    745
    The problem should be solved in Unity 5.4.0b2. You might wanna give it a try! :)
     
    drewradley, daisySa and Todd-Wasson like this.
  23. Todd-Wasson

    Todd-Wasson

    Joined:
    Aug 7, 2014
    Posts:
    1,079
    Good job, David. Thanks. :)
     
    David-Berger likes this.
  24. daisySa

    daisySa

    Joined:
    Dec 29, 2011
    Posts:
    341
    That's fantastic news David, thank you!

    I'm using 5.3.2f1 at the moment but I'll try and install 5.4.0b2 later today on a separate machine and post the results here.
     
    David-Berger likes this.
  25. daisySa

    daisySa

    Joined:
    Dec 29, 2011
    Posts:
    341
    I've tried it in Unity 5.4.0b2 and can confirm that this issue is fixed. :)

    Thanks for your help, David.
     
    David-Berger and Todd-Wasson like this.
  26. HakJak

    HakJak

    Joined:
    Dec 2, 2014
    Posts:
    192
    I was getting this error in 5.3 and all I had to do was regenerate my SpeedTree materials. This fixed the errors for every scene.
     
  27. Deleted User

    Deleted User

    Guest

    Hi! Is it possible to have this fix available for previous versions of Unity like 5.1.4? Thanks in advance!
     
  28. David-Berger

    David-Berger

    Unity Technologies

    Joined:
    Jul 16, 2014
    Posts:
    745
    @F-R That's not possible as we do not update below 5.3.x at this point anymore. You can read about it here. Keep in mind that this is already a couple of months old and we are on 5.3 now and do not support 5.2 either :(
     
  29. Deleted User

    Deleted User

    Guest

    Ok so I'll stay with my dirty fixes and workarounds.
    I wish that one day Unity take some time to make an optimization/cleaning only release with a long term support. Like an island of reliability in this ocean on chaos.
     
    David-Berger likes this.
  30. HakJak

    HakJak

    Joined:
    Dec 2, 2014
    Posts:
    192
    Don't fret F-R... these are a bunch of hard working people that provide an amazing product for FREE to indie devs like us. Everything is a constant sea of change in the world of technology and programming.

    I've been working on a project in my very limited spare time for the past year. Here's how I avoid drowning in the chaos:

    1.) Always back-up your project when you reach major milestones. Always back-up before upgrading.
    2.) Don't upgrade your project with every single Unity release. Wait for the dust to settle, do some research and download the version that seems to be the most stable.
    3.) ALWAYS read the update release notes. Sometimes most of the updates won't even be needed for your current project and you can wait to update.
    4.) Make sure any third party assets are up-to-date and compatible with the version you install as well. Third party assets can be the source of the problem just as often as the Unity engine.

    Hope that helps.
     
  31. Lenakeiz

    Lenakeiz

    Joined:
    Jun 11, 2015
    Posts:
    34
    Can we know more or less when the beta will become a stable release? I am asking because having the exact same problem and I don t have Unity Pro. Using 5.3.3
     
  32. KeithT

    KeithT

    Joined:
    Nov 23, 2011
    Posts:
    83
    Getting the same error in 5.4.0b13

    EnsureRunningOnMainThread can only be called from the main thread.
    Constructors and field initializers will be executed from the loading thread when loading a scene.
    Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.

    When using this client socket code, which works fine in Unity 4.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System;
    4. using System.Net;
    5. using System.Net.Sockets;
    6. using System.Text;
    7.  
    8. public class AsynchClient : MonoBehaviour
    9. {
    10.  
    11.     private Socket _clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    12.     private byte[] _recieveBuffer = new byte[8142];
    13.     //public UITextList textList;
    14.  
    15.     void Awake()
    16.     {
    17.         SetupServer();
    18.     }
    19.  
    20.     private void SetupServer()
    21.     {
    22.         string host = "127.0.0.1";
    23.         int port = 8888;
    24.  
    25.         IPHostEntry hostEntry = null;
    26.         hostEntry = Dns.GetHostEntry(host);
    27.  
    28.         foreach (IPAddress address in hostEntry.AddressList)
    29.         {
    30.             IPEndPoint ipe = new IPEndPoint(address, port);
    31.             try
    32.             {
    33.                 _clientSocket.Connect(ipe);
    34.             }
    35.             catch (SocketException ex)
    36.             {
    37.                 Debug.Log(ex.Message);
    38.             }
    39.  
    40.             if (_clientSocket.Connected)
    41.             {
    42.                 _clientSocket.BeginReceive(_recieveBuffer, 0, _recieveBuffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
    43.  
    44.                 break;
    45.             }
    46.             else
    47.             {
    48.                 continue;
    49.             }
    50.         }
    51.     }
    52.  
    53.     private void ReceiveCallback(IAsyncResult AR)
    54.     {
    55.         //Check how much bytes are recieved and call EndRecieve to finalize handshake
    56.         int recieved = _clientSocket.EndReceive(AR);
    57.  
    58.         if (recieved <= 0)
    59.             return;
    60.  
    61.         //Copy the recieved data into new buffer , to avoid null bytes
    62.         byte[] recData = new byte[recieved];
    63.         Buffer.BlockCopy(_recieveBuffer, 0, recData, 0, recieved);
    64.  
    65.         //Process data here the way you want , all your bytes will be stored in recData
    66.         Debug.Log(Encoding.ASCII.GetString(_recieveBuffer, 0, _recieveBuffer.Length));
    67.  
    68.         //Start receiving again
    69.         _clientSocket.BeginReceive(_recieveBuffer, 0, _recieveBuffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
    70.     }
     
  33. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    try moving this to Awake before you call SetUpServer() ?
     
  34. KeithT

    KeithT

    Joined:
    Nov 23, 2011
    Posts:
    83
    Thanks but's it was not that. The above is the working code from Unity4. There was a difference in the Unity 5 version that had the problem, in that was trying to write to a textlist instead of the debug.log statement at line 66. ReceiveCallback is called in a different thread (by Mono) and it is not allowed to use any Unity functionality from a different thread. Changing it so that the textlist was populated from the main thread solved it.
     
  35. arfan447

    arfan447

    Joined:
    Oct 26, 2015
    Posts:
    7
    Deleteing the tree prefab from hierarchy or terrain tree remove the error....................
     
  36. Gua

    Gua

    Joined:
    Oct 29, 2012
    Posts:
    455
    @F-R, Can you tell what workarounds are you using for this error?
     
  37. Todd-Wasson

    Todd-Wasson

    Joined:
    Aug 7, 2014
    Posts:
    1,079
    It's been fixed. Not sure which version exactly but I'm on 5.3.5p2 and it's not longer an issue here.
     
  38. Araj-Tejani

    Araj-Tejani

    Joined:
    Sep 25, 2015
    Posts:
    5
    You can create own tree in GameObject the Prefabs will generate in Project in this remove component tree the error will gone