Getters

I have this part of a script called ChangeFirection

private bool direction;

public bool Direction
{
    get
    {
        return direction;
    }
}

I want to get the current value of the variable “direction” in another script and then set it to a variable called “currentDirection”

private bool currentDirection;

void Start() {
    ChangeDirection dir = new ChangeDirection();
}

IEnumerator ChooseBlock()
{
    while (true)
    {
        bool currentDirection = dir.direction;
        ...
        ...
        ...
    }
}

The console says there is a problem with the last line “bool currentDirection = dir.direction”
“The name ‘dir’ does not exist in the current context”

private bool currentDirection;
ChangeDirection dir ;
void Start() {
    dir = new ChangeDirection();
}
IEnumerator ChooseBlock()
{
    while (true)
    {
        bool currentDirection = dir.direction;
        ...
        ...
        ...
    }
}

dir goes out of scope when execution leaves Start. Declare dir outside the Start function, and you can access it in ChooseBlock. Also, Direction is capitalised in your code, so try:

    private bool currentDirection;
    ChangeDirection dir;

    void Start()
    {
        dir = new ChangeDirection();
    }

    IEnumerator ChooseBlock()
    {
        while (true)
        {
            bool currentDirection = dir.Direction;
        ...
        ...
        ...
        }
    }

For more information on scope, try here.

“ChangeDirection.direction’ is inaccessible due to its protection level”

__d__irection is the private variable. __D__irection is its public getter.

Ooops missed that, thank you