The question is answered, right answer was accepted
Underwater effect error
You know... I was having some problems with making an underwater effect recently, so i chose to just copy and paste a script that i have found in the SUPPOSEDLY Unity's official Wikia. (Also, you guys should get that thing offline, the last time it's scripts were updated was a long time ago).
I've tried to update, i tried to change some things, but i still get two errors:
This is the script:
using UnityEngine;
using System.Collections;
public class UnderWater : MonoBehaviour
{
//This script enables underwater effects. Attach to main camera.
//Define variable
public int underwaterLevel = 7;
//The scene's default fog settings
private bool defaultFog = RenderSettings.fog;
private Color defaultFogColor = RenderSettings.fogColor;
private float defaultFogDensity = RenderSettings.fogDensity;
private Material defaultSkybox = RenderSettings.skybox;
private Material noSkybox;
void Start()
{
//Set the background color
GetComponent<Camera>().backgroundColor = new Color(0, 0.4f, 0.7f, 1);
}
void Update()
{
if (transform.position.y < underwaterLevel)
{
RenderSettings.fogColor = defaultFogColor;
RenderSettings.fog = true;
RenderSettings.fogColor = new Color(0, 0.4f, 0.7f, 0.6f);
RenderSettings.fogDensity = 0.04f;
RenderSettings.skybox = noSkybox;
}
else
{
RenderSettings.fog = defaultFog;
RenderSettings.fogDensity = defaultFogDensity;
RenderSettings.skybox = defaultSkybox;
}
}
}
Can anyone help me to identify and fix those bugs? Also, if one of you happen to be a WebMaster of the website, you could also edit the page. Here's the original outdated link: Unity Wikia
Ah, and before someone ask "Oh, it is so simple, why didn't you follow the instructions?" Because i am so tired that i can't even think right AND because these lines actually doesn't exist... You know.... seriously.
Answer by Namey5 · Oct 05, 2016 at 03:13 AM
Have you tried moving the initialization stuff inside a function rather than having it with the variable declaration?
//The scene's default fog settings
private bool defaultFog;
private Color defaultFogColor;
private float defaultFogDensity;
private Material defaultSkybox;
private Material noSkybox;
void Awake ()
{
defaultFog = RenderSettings.fog;
defaultFogColor = RenderSettings.fogColor;
defaultFogDensity = RenderSettings.fogDensity;
defaultSkybox = RenderSettings.skybox;
}
Actually, i already tried that, but i am going to give it a try. $$anonymous$$aybe it works.
@Namey5 Nope, didn't worked. I've tried with void start too.
That's odd because the error message seems pretty clear. Have you removed the initialisations from the variable declarations as well as adding them to the Awake function, as Namey5 showed?
Yes, I read your comment. If you search this site for get_fog you'll find several people with the exact same problem, for whom the fix was exactly as Namey5 said. So it is worth pursuing, especially as the error message is telling you what the issue is.
The lines in the error do exist, they're just not lines that you wrote. The error is telling you that when the instance is being created by its constructor ( UnderWater.ctor()
), you're trying to access something that you can't access at that point (because the constructor is run on the loading thread and many of the Unity functions can only be run on the main thread).
It would appear that some or all of those RenderSettings.fog*
properties involve a call to RenderSettings.get_fog()
, and that's one of those things that you can only use on the main thread.
If the code used to work and now doesn't, I suspect it's because things like RenderSettings.fog
used to be simple static variables, but in more recent versions of Unity they are now properties that need to invoke this get_fog
method.
Is it possible that Unity isn't loading your script changes for some reason?
It is indeed strange. There's no real reason why it would work, but just as a failsafe perhaps try it in the update function.
private bool start = true;
void Update ()
{
if (start) {
defaultFog = RenderSettings.fog;
defaultFogColor = RenderSettings.fogColor;
defaultFogDensity = RenderSettings.fogDensity;
defaultSkybox = RenderSettings.skybox;
start = false;
}
...
@Namey5 wtf... It still doesn't work. Could you post the entire script changed? I think i am missing something in here. Also, did you tested it?
Never$$anonymous$$d, i WAS missing something! I was replacing a thing that i wasn't supposed to.
Yes, i already said that 3 times. You guys should practice a little of "expression"...
Anyway, could you make this comment an answer? While you do this i am going to negativate that guy for giving me a $$anonymous$$us!!
I understand that English may not be your first language, but you should know that your comments do come across as both rude and difficult to understand: you have repeatedly stated that the problem is fixed but also that you still need an answer. When others who are trying to help you have asked for clarification, you might want to be more tolerant in your explanation.
All we are trying to do is help you and your issue. I understand that it may be frustrating if someone does not understand your point, but please be patient when asked for clarification as it may be you who is co$$anonymous$$g across as vague.
Follow this Question
Related Questions
Underwater Effect? 3 Answers
Underwater effect 2 Answers
BATIM Highlighting in Unity 1 Answer
Visual effect camera distance size 0 Answers
Light Shadow problem 1 Answer