- Home /
How can i set the sun transform value on the SunShafts script?
The Sun Shafts script is only available to Unity Pro users and is not directly attainable through code, so I figured I would use GetComponent and reflection to set the sunTransform field via code. (Since setting it manually in every scene is painful to maintain).
This is the code I was hoping would work, but it did not:
Component sunShafts = GetComponent("SunShafts");
sunShafts.GetType().GetField("sunTransform").SetValue(sunShafts, SunShaftsCaster.Instance.transform);
Apparently it looks like the code works, it just didn't update the inspector with the value but the value is set in code. However I would like to have some more control over this script and I tried to rewrite it to c# but it has a bunch of shaders I have no idea how it populates... Anyone know?
Answer by grim2594 · Sep 15, 2012 at 12:43 AM
I had to fully convert the scripts to C#, to get it to work with the rest of my assets. (I use only C# scripts.) After you convert all of the scripts, it's very easy to use and change settings/assign objects at runtime. Just make sure you convert all of the dependency scripts, that are used by/for the Sun Shafts. It took me a while to do it, but I am sure happy with the outcome.
You do not have to change the shaders, just the scripts that use them. Ensure that you set the shaders, after converting, in the inspect also.
After it's all complete, you can adjust the values like so:
Camera.mainCamera.GetComponent<SunShafts>().sunTransform = sunLocator.transform;
Camera.mainCamera.GetComponent<SunShafts>().enabled = true;
For the record, you don't actually need to convert the scripts to C#.
See this question for more info: http://answers.unity3d.com/questions/754418/modify-sunshafts-from-c.html#answer-754841
Your answer
![](https://koobas.hobune.stream/wayback/20220613062816im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to make a mirror in 3.x? 0 Answers
Sunshaft Image Effect Help 1 Answer
Problem with Unity 3.5 Image effects 2 Answers
Water4 looks like fog; no surface 1 Answer
Volumetric light and Sun Shafts 3 Answers