Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

X cannot be declared in this scope + X cannot be used before it's declared

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 ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

t looks like the error is caused by the use of the variable name distance both inside and outside of the lambda expression in the Aggregate method. In the lambda expression, the distance variable is declared as a local variable, which means it cannot be used outside of the lambda expression.

One way to fix this error would be to rename the distance variable inside the lambda expression to something else, like this:

var (distance, closestObject) = _foundOrCreatedAnchorGameObjects.Aggregate(
    new Tuple<float, GameObject>(Mathf.Infinity, null),
    (minPair, gameobject) =>
    {
        Vector3 gameObjectPosition = gameobject.transform.position;
        float dist = (position - gameObjectPosition).magnitude;
        return dist < minPair.Item1 ? new Tuple<float, GameObject>(dist, gameobject) : minPair;
    });

Alternatively, you could remove the declaration of the distance variable inside the lambda expression, like this:

var (distance, closestObject) = _foundOrCreatedAnchorGameObjects.Aggregate(
    new Tuple<float, GameObject>(Mathf.Infinity, null),
    (minPair, gameobject) =>
    {
        Vector3 gameObjectPosition = gameobject.transform.position;
        return (position - gameObjectPosition).magnitude < minPair.Item1 ? 
               new Tuple<float, GameObject>((position - gameObjectPosition).magnitude, gameobject) : minPair;
    });

In this case, the distance variable will be assigned the value of the distance between position and gameObjectPosition, rather than being declared as a local variable.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading