I'm following the Microsoft tutorial to create a new HoloLens Unity app using Azure Spatial Anchors and the given code has some errors.
The errors
'distance' cannot be declared in this scope because that name is used in an enclosing local scope
is the first encountered error. I tried to solve it by commenting float
in front of distance
but then I got Cannot use local variable 'distance' before it is declared
Cannot infer the type of implicitly-typed deconstruction variable 'distance'.
private bool IsAnchorNearby(Vector3 position, out GameObject anchorGameObject)
{
anchorGameObject = null;
if (_foundOrCreatedAnchorGameObjects.Count <= 0)
{
return false;
}
//Iterate over existing anchor gameobjects to find the nearest
var (distance, closestObject) = _foundOrCreatedAnchorGameObjects.Aggregate(
new Tuple<float, GameObject>(Mathf.Infinity, null),
(minPair, gameobject) =>
{
Vector3 gameObjectPosition = gameobject.transform.position;
float distance = (position - gameObjectPosition).magnitude;
return distance < minPair.Item1 ? new Tuple<float, GameObject>(distance, gameobject) : minPair;
});
if (distance <= 0.15f)
{
//Found an anchor within 15cm
anchorGameObject = closestObject;
return true;
}
else
{
return false;
}
}
What is wrong in this tutorial's code ?
CodePudding user response:
You have distance
defined twice:
var (distance, closestObject)
and then:
float distance = (position - gameObjectPosition).magnitude;
Rename one or other.