- Home /
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:
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;ArgumentException: get_deltaTime can only be called from the main thread.
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.
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?
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. :)
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();
}