Need to access a script attached to a camera in Unity5

In previous versions of Unity I used:

funcName = camera.GetComponent <scriptName> ();

but Unity says that in Unity5 I now have to use this instead

funcName = GetComponent <Camera> ()

All fine but that only gets me the camera. I need the script attached to the camera, and I can find no documentation on how this new method works.

Can anyone help?

If you have the camera reference.

Camera cam = GetComponent<Camera>();

and you want the script of some name:

ScriptName sn = cam.GetComponent<ScriptName>();

You have gotten a reference to the camera, then attempted to get the component “ScriptName” attached to the camera.

Or use the static reference if your Camera is tagged “MainCamera”

ScriptName sn = Camera.main.GetComponent<ScriptName>();

where main will already reference the tagged “MainCamera” in the scene.

The scruipt is not attached to the Camera component, it is attached to the same gameObject the Camera component is attached to.

So you could write

funcName = GetComponent<Camera>().gameObject.GetComponent<ScriptName>();

But that’s not neccessary, since the ScriptName component is on the same gameobject anyway, so you can directly access it:

funcName = GetComponent<ScriptName>();