Hello dear wonderful Cinemachine devs.
So I try to create multiple virtual cameras, one for each player character in a scene, then want to store them as a List and switch / activate / configure them by an external script.
That does not seem to work for me atm. as multiple solutions just do not work here.
At that, for me, the example scripts / example scenes are not explaining the C# alternative. I am sure you will improve and update that in the future. ![]()
What do I want to achieve in detail?
- Create multiple VCs via code, one for each player character to use of the great blending between VCs. (works)
- Store them into a List, either of type GameObject or CinemachineVirtualCamera, depending on what works in the end. (does not work)
- Access the Cinemachine camera script from an external game manager to activate / deactivate the specific camera linked to the player character (does not work)
- Work with the previously created List / the objects in it via accessing, say, VC [5] in the List. (does not work)
My problems:
The example scene in which you show how to switch between multiple (state-driven) cameras is great to see the effect but it does not show the C# code alternative afaik.
I cannot seem to access the GameObject of the VC to use SetActive(true/false) or other functions.
It both seems not to be allowed to first create the GameObject, store it and then add the virtual camera component and I am not allowed to access the GameObject of the VC.
I seem to have missed some helpful information for this purpose or I just think the wrong way.
My camera code (excerpt):
public CinemachineVirtualCamera vcam;
public List<CinemachineVirtualCamera> vcameras;
void Start(){
vcameras = new List<CinemachineVirtualCamera> ();
foreach (GameObject player in GetComponent<Players>().existingPlayers) {
// Create a virtual camera that looks at object "Cube", and set some settings
vcam = new GameObject ("Virtual Camera").AddComponent<CinemachineVirtualCamera> ();
vcameras.Add (vcam);
}
}
Manager:
void Start(){
foreach (CinemachineVirtualCamera vcamera in GetComponent<CameraManager>().vcameras) {
vcamera.gameObject.SetActive (false);
}
GetComponent<CameraManager> ().vcameras[0].gameObject.SetActive (true);
}
void Update(){
// If player hits a button, switch to next player / VC, for example via:
vcameras[2].gameObject.SetActive(true);
// or a similar, working code
}
(Both scripts are components of the GameManager)
When I try the code this way, I cannot access the gameObject via vcameras[0].gameObject by the external manager.
Error says List is null / array out of range but it is filled with the right amount of cameras in the Inspector.
This error also occurs if I want to access the virtual camera component.
My failed workaround:
First create the vcam as a GameObject, store it in a List, then add the CinemachineVirtualCamera component to it. The error tells me type conflicts between GameObject and CinemachineVirtualCamera.
Can somebody of the rad people here help me further?
Iād appreciate it a lot. Thank you so much.