问题
this problem is unsolvable or what?i don't need to provide any code example it just doesn't work to anyone!and i don't know how its possible,you just have to try this : GetComponent<DepthOfField>().enabled = false;
assume that we have a MainCamera
object and we attach it a DepthOfField
image effect Script.and we create another script named whatever
and attach it to the MainCamera
and in the whatever
script we just call this:
void Start() {
GetComponent<DepthOfField>().enabled = false;
}
shouldn't it work?well it doesn't, it returns null reference exception even if the script is attached to the main camera.is it fixable? and if yes i need it !
回答1:
It should work because they are both attached to the same GameObject. Although, there are many other ways you can try to fix this.
You can just do
DepthOfField myDept;
void Start() {
myDept = GameObject.Find("MainCamera").GetComponent<DepthOfField>();
myDept.enabled = false;
}
Or You could make DepthOfField public then assign the DepthOfField from the Editor. For example,
public DepthOfField myDept;
//Drag and Drop the MainCamera GameObject from the scene to this and it will automatically assign the DepthOfField script to the myDept.
Then you can do
myDept.enabled = false;
For this to work, DepthOfField must be attached to the MainCamera GameObject.
来源:https://stackoverflow.com/questions/30003776/why-getcomponent-returns-null-reference-for-imageeffect