Search Unity

how to access a variable from another method

Discussion in 'Getting Started' started by deathrobot, Apr 8, 2021.

  1. deathrobot

    deathrobot

    Joined:
    Aug 26, 2020
    Posts:
    3
    I know that there are already 2 threads like this but both recommend making it a global variables but I cant because I need a 2d array that uses 2 other variables like this

    Code (CSharp):
    1. public int y;
    2.     public int x;
    3. int[,] map = new int[y, x];
    4. start() {
    5. #blah blah blah
    6.     }
    7. update() {
    8. #blah blah blah
    9.     }
    but this gives the errors
    a field initializer cannot reference the field. method, or property 'spawner.y'
    a field initializer cannot reference the field. method, or property 'spawner.x'
    so unless you know how to fix this I need to access a variable from another method
     
    Last edited: Apr 8, 2021
  2. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    First, when posting code, be sure to use the code tags feature the forum provides. This will format your code and make it easier for the rest of us to read.

    Next, you should also post actual code that you've having a problem with, not some generic placeholder that doesn't actually represent your problem. Here you've mentioned not being able to access spawner.x/y, but haven't shown us how you're actually trying to use that. We can't give useful advice if we don't know what your actual problem is.

    There shouldn't be any problem accessing properties of a class from inside any of its methods. It's possible you're declaring local variables inside of one of your methods, in which case, they are inaccessible from outside that method by design. This is due to how scope and memory work, and trust me, you wouldn't want it to work any other way. But again, because you haven't shared actual code, I can only guess here instead of providing information more specific to you.
     
  3. deathrobot

    deathrobot

    Joined:
    Aug 26, 2020
    Posts:
    3
    but I'm giving you the part of the code that's giving me the error the array that cant access variables x/y is right next to them would you rather I give you the entire code?
     
  4. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    You cannot reference fields until after they've been initialized.
    Code (CSharp):
    1. public int x;
    2. public int y;
    3.  
    4. int[,] map = new int[y, x]; //Will not work. y and x haven't been initialized yet.
    You must do so in a method instead. For MonoBehaviours, the Awake method is likely what you want to use:
    Code (CSharp):
    1. public int x;
    2. public int y;
    3.  
    4. int[,] map;
    5.  
    6. void Awake() {
    7.   map = new int[y, x];
    8. }
     
    deathrobot likes this.
  5. deathrobot

    deathrobot

    Joined:
    Aug 26, 2020
    Posts:
    3
    thank you I realized why it wasn't working but I couldn't figure out how to do it I thought I could define the variable in start but then I realized update couldn't access it this works a lot better thank you