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?
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).
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.