gpt4 book ai didi

variables - 从其他脚本访问变量

转载 作者:行者123 更新时间:2023-12-01 19:59:53 25 4
gpt4 key购买 nike

如何从其他脚本组件调用变量?

mirror.transform.position = cop.GetComponent("AI_Car").mirrorPos;

脚本AI-Car-script中声明的公共(public)变量carPos似乎无法访问:

'mirrorPos' is not a member of 'UnityEngine.Component'.

最佳答案

两件事:

1) 我使用 C# 工作,所以我对此可能是错误的,但我认为您必须获取组件,然后获取变量。例如:

var otherScript: OtherScript = GetComponent("AI_CAR"); 
var newPosition = otherScript.mirrorPos;

2)我认为最好的做法是创建一个临时变量然后访问它。所以在上面的例子中,我会像这样改变mirror.transform.position:

mirror.transform.position = newPosition;

显然,在 vars 中工作并不总是很好(有时是这样,这是一个完全不同的对话),但这只是一个简单的伪代码示例。希望这有帮助!

编辑:here是文档

关于variables - 从其他脚本访问变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30311674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com