Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Flizzehh · Jan 04, 2014 at 06:42 PM · javascriptclassfloattime.deltatime

Error: "ArgumentException: get_deltaTime can only be called from the main thread"

I'm trying to make a weather system that randomly sets the weather every 15 minutes. To make the delay there are 2 variables, "weatherChangeDelayCurrent" which is the current time remaining of the delay, and "weatherChangeDelayRate" which is the rate at which the current variable changes. I subtract the rate from current by time.deltaTime and this error shows up. I haven't been able to find out why the error is happening, but I am kind of new to this so I'm sorry if its something super simple.

This is the full error:

ArgumentException: get_deltaTime 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. The error points to this line where the time is being reduced until the next time it chooses the weather: weatherChangeDelayCurrent -= weatherChangeDelayRate Time.deltaTime; Here is the class that has the error in it, the error line is right below the Weather function: public class Weather { var fogDensity : float; var emissionRate : float; var snowParticleSystem : GameObject;
var snowDensityMax : float = 0.1; var snowDensityMin : float = 0.0001; var snowDensityRate : float = 0.1; var snowDensityToClear : boolean; var snowDensityToLight : boolean; var snowDensityToMedium : boolean; var snowDensityToHeavy : boolean; var snowDensityToStorm : boolean; var snowDensityCanChange : boolean = true;
var weatherArray = new Array (0,1,2,3,4); var weatherChangeDelayMax : float = 0.1; var weatherChangeDelayCurrent : float = 0.1; var weatherChangeDelayRate : float = 0.1111111111111111; var weatherArrayRandom;
var weatherTypeClear : boolean; var weatherTypeLightSnow : boolean; var weatherTypeMediumSnow : boolean; var weatherTypeHeavySnow : boolean; var weatherTypeSnowstorm : boolean;
public function Weather () { weatherChangeDelayCurrent -= weatherChangeDelayRate
Time.deltaTime; // print(weatherChangeDelayCurrent); if (weatherChangeDelayCurrent <= 0) { weatherChangeDelayCurrent = 0; weatherArrayRandom = weatherArray[Random.Range(0,weatherArray.length)]; if (weatherArrayRandom == 0) // Clear weather { weatherTypeClear = true; weatherTypeLightSnow = false; weatherTypeMediumSnow = false; weatherTypeHeavySnow = false; weatherTypeSnowstorm = false; print("Clear"); } if (weatherArrayRandom == 1) // Light Snow { weatherTypeClear = false; weatherTypeLightSnow = true; weatherTypeMediumSnow = false; weatherTypeHeavySnow = false; weatherTypeSnowstorm = false; print("Light"); } if (weatherArrayRandom == 2) // Medium Snow { weatherTypeClear = false; weatherTypeLightSnow = false; weatherTypeMediumSnow = true; weatherTypeHeavySnow = false; weatherTypeSnowstorm = false; print("Medium"); } if (weatherArrayRandom == 3) // Heavy Snow { weatherTypeClear = false; weatherTypeLightSnow = false; weatherTypeMediumSnow = false; weatherTypeHeavySnow = true; weatherTypeSnowstorm = false; print("Heavy"); } if (weatherArrayRandom == 4) // Snowstorm { weatherTypeClear = false; weatherTypeLightSnow = false; weatherTypeMediumSnow = false; weatherTypeHeavySnow = false; weatherTypeSnowstorm = true; print("Snowstorm"); } weatherChangeDelayCurrent = weatherChangeDelayMax; } if (weatherTypeClear == true) { // snowParticleSystem.particleEmitter = emissionRate = 1000; RenderSettings.fogDensity += snowDensityRate * Time.deltaTime; } if (RenderSettings.fogDensity > snowDensityMax) { snowDensityCanChange = false; RenderSettings.fogDensity = snowDensityMax; } if (RenderSettings.fogDensity < snowDensityMin) { snowDensityCanChange = false; RenderSettings.fogDensity = snowDensityMin; } } } public var WeatherVariables : Weather; The Weather function is being called from the Update function. There is more code in this script but I only put in the class that has the issue in it. Tell me if you want the entire script and I'll post it as soon as I can. The whole thing is still a work in progress so thats why there are unfinished bits and things that are commented out.
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image iwaldrop · Jan 04, 2014 at 07:02 PM 0
Share

The error indicates that you're trying to access Unity runtime stuff from another thread...you're not doing any manual threading ops, are you?

avatar image Flizzehh · Jan 04, 2014 at 07:40 PM 0
Share

Thanks but I managed to fix it.

avatar image iwaldrop · Jan 04, 2014 at 07:41 PM 0
Share

Awesome! Glad you got it working. If you wouldn't $$anonymous$$d posting your fix or deleting your question that would be fantastic. It's not much help to anybody to have an unanswered question lingering about. :)

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Flizzehh · Jan 04, 2014 at 08:10 PM

I ended up fixing it by changing the name of the function to "Weatherf" and changing it to that in the Update function as well, and then putting the "WeatherVariables" variable which gets the variables from the Weather class and put it in front of all of the variables in the Weatherf function.

Here is the new code:

     public class Weather
     {
         var fogDensity : float;
         var emissionRate : float;
         var snowParticleSystem : GameObject;
         
         var snowDensityMax : float = 0.1;
         var snowDensityMin : float = 0.0001;
         var snowDensityRate : float = 0.1;
         var snowDensityToClear : boolean;
         var snowDensityToLight : boolean;
         var snowDensityToMedium : boolean;
         var snowDensityToHeavy : boolean;
         var snowDensityToStorm : boolean;
         var snowDensityCanChange : boolean = true;
         
         var weatherArray = new Array (0,1,2,3,4);
         var weatherChangeDelayMax : float = 0.1;
         var weatherChangeDelayCurrent : float = 0.1;
         var weatherChangeDelayRate : float = 0.1111111111111111;
         var weatherArrayRandom;
         
         var weatherTypeClear : boolean;
         var weatherTypeLightSnow : boolean;
         var weatherTypeMediumSnow : boolean;
         var weatherTypeHeavySnow : boolean;
         var weatherTypeSnowstorm : boolean;
     }
         function Weatherf ()
         {
             WeatherVariables.weatherChangeDelayCurrent -= WeatherVariables.weatherChangeDelayRate * Time.deltaTime;
 //            print(weatherChangeDelayCurrent);
             if (WeatherVariables.weatherChangeDelayCurrent <= 0)
             {
                 WeatherVariables.weatherChangeDelayCurrent = 0;
                 WeatherVariables.weatherArrayRandom = WeatherVariables.weatherArray[Random.Range(0,WeatherVariables.weatherArray.length)];
                 if (WeatherVariables.weatherArrayRandom == 0) // Clear weather
                 {
                     WeatherVariables.weatherTypeClear = true;
                     WeatherVariables.weatherTypeLightSnow = false;
                     WeatherVariables.weatherTypeMediumSnow = false;
                     WeatherVariables.weatherTypeHeavySnow = false;
                     WeatherVariables.weatherTypeSnowstorm = false;
                     print("Clear");
                 }
                 if (WeatherVariables.weatherArrayRandom == 1) // Light Snow
                 {
                     WeatherVariables.weatherTypeClear = false;
                     WeatherVariables.weatherTypeLightSnow = true;
                     WeatherVariables.weatherTypeMediumSnow = false;
                     WeatherVariables.weatherTypeHeavySnow = false;
                     WeatherVariables.weatherTypeSnowstorm = false;
                     print("Light");
                 }
                 if (WeatherVariables.weatherArrayRandom == 2) // Medium Snow
                 {
                     WeatherVariables.weatherTypeClear = false;
                     WeatherVariables.weatherTypeLightSnow = false;
                     WeatherVariables.weatherTypeMediumSnow = true;
                     WeatherVariables.weatherTypeHeavySnow = false;
                     WeatherVariables.weatherTypeSnowstorm = false;
                     print("Medium");
                 }
                 if (WeatherVariables.weatherArrayRandom == 3) // Heavy Snow
                 {
                     WeatherVariables.weatherTypeClear = false;
                     WeatherVariables.weatherTypeLightSnow = false;
                     WeatherVariables.weatherTypeMediumSnow = false;
                     WeatherVariables.weatherTypeHeavySnow = true;
                     WeatherVariables.weatherTypeSnowstorm = false;
                     print("Heavy");
                 }
                 if (WeatherVariables.weatherArrayRandom == 4) // Snowstorm
                 {
                     WeatherVariables.weatherTypeClear = false;
                     WeatherVariables.weatherTypeLightSnow = false;
                     WeatherVariables.weatherTypeMediumSnow = false;
                     WeatherVariables.weatherTypeHeavySnow = false;
                     WeatherVariables.weatherTypeSnowstorm = true;
                     print("Snowstorm");
                 }
                 WeatherVariables.weatherChangeDelayCurrent = WeatherVariables.weatherChangeDelayMax;
             }
             if (WeatherVariables.weatherTypeClear == true)
             {
 //                snowParticleSystem.particleEmitter = emissionRate = 1000;
                 RenderSettings.fogDensity += WeatherVariables.snowDensityRate * Time.deltaTime;
             }
             if (RenderSettings.fogDensity > WeatherVariables.snowDensityMax)
             {
                 WeatherVariables.snowDensityCanChange = false;
                 RenderSettings.fogDensity = WeatherVariables.snowDensityMax;
             }
             if (RenderSettings.fogDensity < WeatherVariables.snowDensityMin)
             {
                 WeatherVariables.snowDensityCanChange = false;
                 RenderSettings.fogDensity = WeatherVariables.snowDensityMin;
             }
         }
         public var WeatherVariables : Weather;

And this is what the Update function looks like, even though it should be obvious:

 function Update ()
 {
     Weatherf();
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Create an array of classes, of different type. 1 Answer

Are Unity's JS OOP functions customized? 2 Answers

Referencing subclass in Unity JS 2 Answers

Creating a dynamic array of objects of custom class 1 Answer

How can I declare class properties of an object in a single line? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges