Search code examples
c#unity-game-enginecinemachine

how to reference virtual camera cinemachine unity


i was doing unity project with cinemachine in 2D, and the code is actually work but everytime i press play, the virtual cam dissapear in player script reference, here take a lookenter image description here

as you can see in player control there is a variable/reference for virtual cam but when i press play enter image description here and boom the camera is dissapear from its reference

i tried to find the camera using GameObject.findobjectwithtag but its says an error that basicaly said "cannot convert Gameobject to virtual camera". is there any other ways to reference virtual camera?


Solution

  • I couldn't leave a comment because of my reputation, but I would like to say that you need to share more information and code examples so that we can help. Anyway, if you want to use GameObject.FindGameObjectWithTag() or something like that, it returns a GameObject, and you can reach the component you wanted like:

    var gameObj = GameObject.FindGameObjectWithTag("awesomeObj");
    var requiredComponent = gameObj.GetComponent<CinemachineVirtualCamera>();
    

    but i do not recommend it, if you can share your code, maybe we can see what causes that mistake.