Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

FadeOut script getting error

Discussion in 'Scripting' started by Hybris_Team, Feb 29, 2016.

  1. Hybris_Team

    Hybris_Team

    Joined:
    Oct 17, 2015
    Posts:
    114
    The script below fades out to black when you press a key, and loads a level. It works, but I receive these two errors. Also, if I have the level to be loaded opened initially I do not receive the error, which further confuses me. Thanks guys.

    Code:
    Code (JavaScript):
    1.  #pragma strict
    2. // FadeInOut
    3. // Variables
    4. var fadeTexture : Texture2D;
    5. var fadeSpeed = 0.2;
    6. var drawDepth = -1000;
    7.  
    8. // On off switch variable
    9. var fadeOut : boolean = false;
    10.  
    11. private var alpha = 0.0;
    12. private var fadeDir = -1;
    13. function Start ()
    14. {
    15.     fadeOut = false;
    16. }
    17. function Update ()
    18. {
    19.     if (Input.GetKeyDown("f") && loadLevel == true)
    20.     {
    21.         //Load Level
    22.         LoadLevel();
    23.     }
    24.  
    25. }
    26. function OnGUI(){
    27.     // Fade out and load outdoor level
    28.     if (fadeOut == true)
    29.     {
    30.         // Fade Out
    31.         alpha -= fadeDir * fadeSpeed * Time.deltaTime;
    32.         alpha = Mathf.Clamp01(alpha);
    33.         GUI.color.a = alpha;
    34.         GUI.depth = drawDepth;
    35.    
    36.         GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), fadeTexture);
    37.      
    38.     }
    39. }
    40.  
    41. var loadLevel : boolean = true;
    42.  
    43. function LoadLevel()
    44. {
    45.     fadeOut = true;
    46.     loadLevel = false;
    47.     yield WaitForSeconds (1);
    48.     Application.LoadLevel("Scene_1_2048");
    49. }
    Errors:
    1.
    GetLocalizedString 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.
    2.
    ArgumentException: GetLocalizedString 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.
    TreeEditor.TreeGroupRoot..cctor ()
    Rethrow as TypeInitializationException: An exception was thrown by the type initializer for TreeEditor.TreeGroupRoot
     
  2. Gohan1

    Gohan1

    Joined:
    Nov 20, 2015
    Posts:
    28
    the moment you used javascript...(insert text here)
     
  3. bdev

    bdev

    Joined:
    Jan 4, 2011
    Posts:
    656
    It sounds like its an editor issue. that main thread buisness is ushually a product of something happening within a finalizer or using something like initializing a variable to Camera.current or some other unity class.

    Whats the stack trace of the error?