- Home /
Referencing DOF in a script? (Unity 5)
Hey guys, I am trying to make a script to change the object that DOF focuses on dynamically within my script. Its working smoothly other than the fact I can't actually reference the DepthOfField script within my script. Any suggestions?
using UnityEngine;
using System.Collections;
public class DOFCameraFocusScript : MonoBehaviour {
public float maxFocusDistance = 1000;
public GameObject camera;
public Component depthOfFieldScript;
// Update is called once per frame
void Update ()
{
if (camera == null)
{
camera = Camera.current.gameObject;
}
depthOfFieldScript = camera.GetComponent <DepthOfField> ();
RaycastHit hit;
if (Physics.Raycast(camera.transform, camera.transform.TransformDirection, out hit, maxFocusDistance))
{
DepthOfField.focalTransform = hit.collider.gameObject;
}
}
}
Error on line 26? That's a basic program$$anonymous$$g problem. Read about types vs. variables.
If you would read the title: Line 26 does give an error, but that is not the problem I'm experiencing. The problem ( if you are using unity 5 and have the DOF script) is that even when I use GetComponent(), it is not able to find the script (Even though the script is called "DepthOfField"). I just realized it does say DepthOfField in line 26, but I tried doing that after having depthOfFieldScript there in the first place.
Answer by sparkzbarca · Mar 10, 2015 at 07:00 PM
yea line 26.
DepthOfField is a class but it's just a class.
its not the actual cameras depth of field it's just a definition of what depth of fields are and how you can change them. This is the blueprint for ford mustangs for example not your ford mustang.
Something like camera.main.depthoffield is how your going to get the actual cameras depth of field.
Thanks for the explanation, but that does not work. Just tried it.
Actually, I need to activate Unity 5's DOF class because I want to assign the focus target on that script. Thanks for trying to help though.
Answer by alsharefeeee · Nov 01, 2015 at 01:24 PM
Declare: public UnityStandardAssets.ImageEffects.DepthOfField dof;
Awake: dof = GetComponent();
Answer by Riacosta · Jan 25, 2016 at 12:29 PM
You need to copy the script to your assets folder (or any subfolder in it) and then reference the namespace UnityStandardAssets.ImageEffects. Then you'll be able to reference the script.
Your answer
Follow this Question
Related Questions
Best way to dynamically assign a camera? 0 Answers
2.5d Side Scroller Dynamic Camera Zoom 1 Answer
game grid generating whle in camera view ? 3d game scene) android 3 Answers
Getting the Camera to Match the Size of the canvas with dynamically added UI elements 1 Answer
Post Processing Depth of Field in 2D 0 Answers