SunShafts not working when adding it by code [Possible Bug?]

Hi! I’m getting a weird error at line 88 with the SunShafts.cs script.

This ONLY happens when I add the component by code, if I attach it to an object in the editor and run it, it works just fine, this is how I’m adding it btw.

gameObject.AddComponent.<UnityStandardAssets.ImageEffects.SunShafts>();

Now why I’m adding it by code and not as normal? because I have in-game settings, I tried destroying it instead of adding it but! I get another weird issue, even when the script doesn’t exist on scene, if it ran just 1 frame (enough time before I destroy it) then the game performance decreases, this is hard to explain, but I posted some weeks ago about that weird problem, so what could be going wrong here?

Thanks in advance!

Can you post the line giving the error?

Line 88 says this:

sunShaftsMaterial.SetVector ("_BlurRadius4", new Vector4 (1.0f, 1.0f, 0.0f, 0.0f) * sunShaftBlurRadius );

So I assume sunShaftsMaterial is null, but this is loaded at line 49 which says:

sunShaftsMaterial = CheckShaderAndCreateMaterial (sunShaftsShader, sunShaftsMaterial);

Was going to inspect this function “CheckShaderAndCreateMaterial” but don’t see where is defined actually, so it seems is returning null. :stuck_out_tongue:

It’s in the base class. This script isn’t maintained by my team, but at a guess the sunShafts Shader is set as a default on the script (select the script and look in the inspector, there will be a set of ‘defaults’).

You might need to do some minor maintenence.
*Move the shader into a resources folder
*Make sunShaftsShader that does a Shader.Find for the shader

We do this in the new image effects, take a look there if you want to see how it’s done. Setting defaults like this in the inspector is nice, but it only works when you do things in the editor.

You are right XD now is solved, the answer was on this old thread too, a shame I didn’t google enough http://answers.unity3d.com/questions/487599/addcomponent-doesnt-work-with-post-effects.html

Thanks a lot now all is working good :smile:

Would it be possible to make the CS version of unity 5 to work on unity4? I really need to setup the ray caster via script and I’m totally unable to do so. Even enable/dissable the shafts is a pain. I can only enable and for whatever reason it does not dissable…

I tested a cs version witch should have gone fine but id did not, it did not allow to enable. Found a CS version that prints shame issue as related in this post

I use for this project u 4.7.2

Drag sun shaft component in the inspector(js version).
I have tested in my android game without problem

public SunShaft shaft;

void Start()
{


    shaft.enabled = false;

}

OMG! tx! I did not it was possible to acces this way!

I made the CS version work, for some reason it was not finding the shaders so in debug mode I assigned them manually to the camera prefab.