Невозможно использовать локальную переменную «hitInfo» до ее объявления - PullRequest
0 голосов
/ 14 июня 2019

После обновления моего проекта до последней версии 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))

Что такоене так с кодом?

1 Ответ

1 голос
/ 14 июня 2019

Вы должны объявить (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))
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...