Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Getters

Discussion in 'Scripting' started by Deleted User, Sep 21, 2015.

  1. Deleted User

    Deleted User

    Guest

    I have this part of a script called ChangeFirection
    Code (CSharp):
    1. private bool direction;
    2.  
    3. public bool Direction
    4. {
    5.     get
    6.     {
    7.         return direction;
    8.     }
    9. }
    I want to get the current value of the variable "direction" in another script and then set it to a variable called "currentDirection"
    Code (CSharp):
    1. private bool currentDirection;
    2.  
    3. void Start() {
    4.     ChangeDirection dir = new ChangeDirection();
    5. }
    6.  
    7. IEnumerator ChooseBlock()
    8. {
    9.     while (true)
    10.     {
    11.         bool currentDirection = dir.direction;
    12.         ...
    13.         ...
    14.         ...
    15.     }
    16. }
    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"
     
  2. Simo

    Simo

    Joined:
    Sep 16, 2012
    Posts:
    85
    Code (CSharp):
    1. private bool currentDirection;
    2. ChangeDirection dir ;
    3. void Start() {
    4.     dir = new ChangeDirection();
    5. }
    6. IEnumerator ChooseBlock()
    7. {
    8.     while (true)
    9.     {
    10.         bool currentDirection = dir.direction;
    11.         ...
    12.         ...
    13.         ...
    14.     }
    15. }
     
  3. Smingleigh

    Smingleigh

    Joined:
    Nov 24, 2012
    Posts:
    9
    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:

    Code (csharp):
    1.  
    2.     private bool currentDirection;
    3.     ChangeDirection dir;
    4.  
    5.     void Start()
    6.     {
    7.         dir = new ChangeDirection();
    8.     }
    9.  
    10.     IEnumerator ChooseBlock()
    11.     {
    12.         while (true)
    13.         {
    14.             bool currentDirection = dir.Direction;
    15.         ...
    16.         ...
    17.         ...
    18.         }
    19.     }
    20.  
    For more information on scope, try here.
     
  4. Deleted User

    Deleted User

    Guest

    "ChangeDirection.direction' is inaccessible due to its protection level"
     
  5. Smingleigh

    Smingleigh

    Joined:
    Nov 24, 2012
    Posts:
    9
    direction is the private variable. Direction is its public getter.
     
  6. Deleted User

    Deleted User

    Guest

    Ooops missed that, thank you