Access function from another script (Main Camera Script)

Hello, I have script that is assigned to the Main Camera and I want to access a function from this script to another script.

So it looks like that :

//Script A: CameraMovement

//Attached to Main Camera

public class CameraMovement : MonoBehaviour {

void myFunctionA() {
	DebugLog("Hello");
}

}

//Script B

public class CameraMovement : MonoBehaviour {
private CameraMovement myAccess;

void Start {
	myAccess = GetComponent<CameraMovement> ();
	//I tried also
	//myAccess = Camera.mainCamera.GetComponent<CameraMovement> ();
	//It doesn't work
}

//Here I try to acces myFunctionA
void Update {
	myAccess.myFunctionA();
}

}

It simply doesnt work, I don’t know how to do it right. Can you help me?

Either make a myMainCamera variable (it can be called anything) in script b like this:

public Camera myMainCamera;

then drag the main camera onto the empty slot in the inspector and change your myAccess line to this:

myAccess = myMainCamera.GetComponent<CameraMovement> ();

Or do it all in that by finding the main camera (it needs to still be called Main Camera so if you changed the name then this line needs the new name to find).

myAccess = GameObject.Find("Main Camera").GetComponent<CameraMovement> ();

either way is fine.

You do it this way because that script could be attached to more than one Camera so you need to identify which object the script is attached to first, even if there’s only one camera.