- Home /
How to modify variables from scripts in standard package?
Hi,
I have a main camera attached with lots of Image Effects, enabled from standard packages, such as CameraMotionBlur, SSAO, and Depth of Field to say a few.
My problem is, I cannot get the script objects (Ex.: GetComponent(DepthOfFieldScatter)), therefore I cannot modify their variables nor turning on/off the scripts.
However, it is completely fine with SSAOEffect. The fact that I found is, SSAOEffect script is a child of MonoBehaviour, while others' parent class is PostEffectsBase. If I have scripts or attach a script from the package with MonoBehaviour, yes it can be accessed normally.
Any idea?
Thanks.
EDIT: It might be very helpful if anyone who follow this thread could try on their own if they're willing to:
Use Unity Pro/Pro trial 4.x (4.3 to be exact)
Create a new project and add Image Effects (Pro only) package.
Add SSAOEffect (Add Component -> Image Effects -> Rendering -> Screen Space Ambient Occlusion)) and add DepthOfFieldScatter (Add Component -> Image Effects -> Camera -> Depth of Field (Lens Blur, Scatter, DX11)) to main camera.
Try to access both scripts in your own behavior script.
Some components can't be changed within script if that's what you are trying to get at. I learned this when trying to mess with a halo component in script. Sorry.
The name in the menu does not have to be the same name of the class, make sure you're using the correct class name.
@Benproductions1 Tried both and none works, it's the first thing I did.
Are you trying to access it from a higher compilation level or on the same compilation level from a different language?
@Benproductions1 I don't understand what you mean by compilation level, but if you mean compatibility level, it's .NET 2.0 subset. What I know is I'm using C# and the scripts that are not working so far (like Camera$$anonymous$$otionBlur, DepthOfFieldScatter, etc.) are using .js. Could that be the problem? I also tried using my own .js file it still have the same limited access like when I use the C# one.
Answer by KellyThomas · Jan 12, 2014 at 03:49 PM
The scripts that you want to access are javascript, there are "issues" with manipulating them from c#.
I found that if I ran the following code:
void Start () {
Component[] components = GameObject.Find("Main Camera").GetComponents<Component>();
foreach (Component c in components) {
Debug.Log(c.GetType());
}
}
Then I received the following output:
So the components are real and accessible, with a type that can be recognised at runtime.
From here I see two options:
1) Reflection
We can use reflection to manipulate the component.
For example:
using System.Reflection;
// ....
void Start () {
Component dofs = GameObject.Find("Main Camera").GetComponent("DepthOfFieldScatter");
FieldInfo fi = dofs.GetType().GetField("focalLength");
fi.SetValue(dofs, 99.0f);
}
2) SendMessage
We can use SendMessage()
to call methods of the component. This is not immediately useful in this case but can be made to work by adding setters to the target scripts.
void Start () {
Component dofs = GameObject.Find("Main Camera").GetComponent("DepthOfFieldScatter");
dofs.SendMessage("SetFocalLength", 99.0f)
}
Well it's quite clear that I still have to use some tricks for the problem, but these are the best tricks so far. I'll check on both performance and see which I will use. Thanks.
Answer by jemonsuarez · Mar 16, 2015 at 06:35 AM
There is an easy way:
//C#
using UnityStandardAssets.ImageEffects;
//Javascript:
import UnityStandardAssets.ImageEffects;
Then access the scripts normally (With object.GetComponent(Effect)
)
$$anonymous$$uch better than reflection. Thanks a lot!
Thank you! I knew it was something like this but it took forever to find it online