将项目更新为最新版本的unity3d后,出现此错误 “错误CS0841:在声明它之前无法使用局部变量'hitInfo' “
if (Physics.Linecast(head.position, vector, out hitInfo, sphereSensor.obstacleLayer) && Physics.Linecast(head.position, vector2, out hitInfo, sphereSensor.obstacleLayer) && Physics.Linecast(head.position, colliderTarget.bounds.center, out RaycastHit hitInfo, sphereSensor.obstacleLayer))
代码有什么问题?
答案 0 :(得分:1)
您必须在条件中更早声明(Type variableName
,因为它们是从左到右求值的,然后在第一个条件(Physics.Linecast(head.position, vector, out hitInfo, sphereSensor.obstacleLayer)
)中,变量hitInfo
不声明还不存在。
将代码更改为此:
// Notice this ---------------------------------v--------v
if (Physics.Linecast(head.position, vector, out RaycastHit hitInfo, sphereSensor.obstacleLayer)
&& Physics.Linecast(head.position, vector2, out hitInfo, sphereSensor.obstacleLayer)
&& Physics.Linecast(head.position, colliderTarget.bounds.center, out hitInfo, sphereSensor.obstacleLayer))
// Type was removed there -------------------------------------------^
或在之前声明变量:
RaycastHit hitInfo;
if (Physics.Linecast(head.position, vector, out hitInfo, sphereSensor.obstacleLayer)
&& Physics.Linecast(head.position, vector2, out hitInfo, sphereSensor.obstacleLayer)
&& Physics.Linecast(head.position, colliderTarget.bounds.center, out hitInfo, sphereSensor.obstacleLayer))