• 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
1
Question by zeuo · Jan 11, 2011 at 04:15 AM · exceptionargumentexception

ArgumentException: get_fog can only be called from the main thread

i get this error. does anyone know how to fix it?

ArgumentException: get_fog 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.

here is the script

//This script enables underwater effects. Attach to main camera.

//Define variables var underwaterLevel = 7;

//The scene's default fog settings var defaultFog = RenderSettings.fog; private var defaultFogColor = RenderSettings.fogColor; private var defaultFogDensity = RenderSettings.fogDensity; private var defaultSkybox = RenderSettings.skybox; var noSkybox : Material;

function Start () {

 //Set the background color
 camera.backgroundColor = Color (0, 0.4, 0.7, 1);

}

function Update () { if (transform.position.y < underwaterLevel) { RenderSettings.fog = true; RenderSettings.fogColor = Color (0, 0.4, 0.7, 0.6); RenderSettings.fogDensity = 0.04; RenderSettings.skybox = noSkybox; }

 else {
     RenderSettings.fog = defaultFog;
     RenderSettings.fogColor = defaultFogColor;
     RenderSettings.fogDensity = defaultFogDensity;
     RenderSettings.skybox = defaultSkybox;
 }

}

Comment
Add comment
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

1 Reply

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

Answer by · Jan 11, 2011 at 05:00 AM

Searching the error revealed two similar issues:

  • ArgumentException: get_value can only be called from the main thread
  • ArgumentException: get_time can only be called from the main thread

Eric's answer is more relevant for your Unityscript:

Code outside of functions should stick to declaring types or specific values only. Instead of

private var attackTime = Time.time;

use

private var attackTime : float;

Basically, you can't store 'RenderSettings.###' from outside of a function. Set the values in function Start() and the error should disappear.


Your code should look something like this. It's untested, but I checked the types against the RenderSettings Script Reference, so it should be fine. Please comment back if you have trouble.

//The scene's default fog settings var defaultFog : bool; private var defaultFogColor : Color; private var defaultFogDensity : float; private var defaultSkybox : Material; var noSkybox : Material;

function Start () {

 defaultFog = RenderSettings.fog;
 defaultFogColor = RenderSettings.fogColor;
 defaultFogDensity = RenderSettings.fogDensity;
 defaultSkybox = RenderSettings.skybox;

 //Set the background color
 camera.backgroundColor = Color (0, 0.4, 0.7, 1);

}

Comment
Add comment · Show 5 · 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
avatar image zeuo · Jan 11, 2011 at 06:11 AM 0
Share

i know that u have to do that but i dont know how to do it. i tried moving all of the private var stuff into the function start but that did not work

avatar image · Jan 11, 2011 at 11:51 AM 0
Share

I've updated the answer with example code. As an aside, it seems pretty inefficient to be setting the RenderSettings every frame. Perhaps you could check if you were underwater last frame, before applying the new settings.

avatar image zeuo · Jan 14, 2011 at 06:19 PM 0
Share

it workss tyyy u soooo muchhh

avatar image Ckmlee · Mar 16, 2013 at 05:42 PM 0
Share

i know nothing about scripting, and is a newbie in Unity. I tried the codes u have and it says the name 'bool' does not denote a valid type ('not found'). So do I need to set the type in the beginning? How can I do that?

avatar image dinhxuanhop · May 09, 2013 at 03:06 AM 0
Share

thanks a lot

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

ArgumentException: get_fog can only be called from the main thread(C#) 2 Answers

Why does List.ToArray throw ArgumentException? 1 Answer

Strange error when calling WWW within a GUI element? 2 Answers

Checkpoint script problems - C# 1 Answer

System.ArgumentException: FindObjectsOfType can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene. 0 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