英文:
how to reference virtual camera cinemachine unity
问题
我在使用Cinemachine进行2D的Unity项目,代码实际上是有效的,但每次按播放按钮时,虚拟摄像机在玩家脚本引用中消失。在这里看一下
正如你在玩家控制中看到的,有一个用于虚拟摄像机的变量/引用,但当我按播放按钮时
然后摄像机就从引用中消失了。
我尝试使用GameObject.findobjectwithtag找到摄像机,但它报错说“无法将GameObject转换为虚拟摄像机”。有其他方法引用虚拟摄像机吗?
英文:
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 look
as you can see in player control there is a variable/reference for virtual cam but when i press play
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?
答案1
得分: 1
我无法留下评论因为我的声誉,但我想说你需要分享更多信息和代码示例,这样我们才能帮助。无论如何,如果你想使用GameObject.FindGameObjectWithTag()或类似的东西,它会返回一个GameObject,然后你可以像这样访问所需的组件:
var gameObj = GameObject.FindGameObjectWithTag("awesomeObj");
var requiredComponent = gameObj.GetComponent<CinemachineVirtualCamera>();
但我不建议这样做,如果你能分享你的代码,也许我们可以看到是什么导致了这个错误。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论