Search Unity

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

Discussion in 'Image Effects' started by earrgames, Apr 26, 2016.

  1. earrgames

    earrgames

    Joined:
    Jan 27, 2014
    Posts:
    168
    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.

    Code (JavaScript):
    1. 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!
     
  2. Zenix

    Zenix

    Joined:
    Nov 9, 2009
    Posts:
    213
    Can you post the line giving the error?
     
  3. earrgames

    earrgames

    Joined:
    Jan 27, 2014
    Posts:
    168
    Line 88 says this:
    Code (CSharp):
    1. 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:
    Code (CSharp):
    1. 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. :p
     
  4. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,225
    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.
     
  5. earrgames

    earrgames

    Joined:
    Jan 27, 2014
    Posts:
    168
  6. TooManySugar

    TooManySugar

    Joined:
    Aug 2, 2015
    Posts:
    864
    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
     
  7. UnityLighting

    UnityLighting

    Joined:
    Mar 31, 2015
    Posts:
    3,874

    Drag sun shaft component in the inspector(js version).
    I have tested in my android game without problem
    Code (CSharp):
    1. public SunShaft shaft;
    2.  
    3. void Start()
    4. {
    5.  
    6.  
    7.     shaft.enabled = false;
    8.  
    9. }
     
  8. TooManySugar

    TooManySugar

    Joined:
    Aug 2, 2015
    Posts:
    864
    OMG! tx! I did not it was possible to acces this way!
     
  9. TooManySugar

    TooManySugar

    Joined:
    Aug 2, 2015
    Posts:
    864
    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.