How to access a script/script value from the GameObject
Sorry if this has been answered before, or if its just a dumb question in general. I can't find anything related to it...
Here is my issue. Say I have a camera with the default Tilt Shift script, and when I aim my sights down, it increases the blur amount. They are both on the exact same GameObject. I know you can do some GetComponent tricks to get it, but how do I actually alter these values?
I assume I will have to create a second one with high blur, and just activate and deactivate it when aiming my sights. It would be nice if I could set-up some type of gradual change however.
So in general, I want to directly change a scripts value, from another script, on the exact same GameObject. I'm brand new, so sorry again if it's simplistic and easy to do...
Answer by etaxi341 · Aug 30, 2016 at 02:24 PM
You could acces the Script "TiltShift" in your own Script for aiming. There you can just change the value of the blur area.
so you have to get the Script like this:
TiltShift ts = GetComponent<TiltShift>();
and then acces the value:
ts.blurArea = yourValue;
and thats it
Thank you, but the issue still lies
It throws errors, and tells me i'm trying to access a destroyed object
Can you show a bit of your code where you are accessing it? Are you maybe defining the TiltShift instance outside of the method you are using it?
Same object, which is "Gun Camera"
Ohh I see whats wrong. You have to define the TiltShift outside of a method and then assign it in Start()
TiltShift ts;
void Start()
{
ts = GetComponent<TiltShift>();
}
Still throws the exact same error.
I looked in the TiltShift code , and saw it had no public class. When I add it in however, it breaks it in another way