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

The Name Does Not Exist In the Current Context, Even Though I Just Declared It

Discussion in 'Scripting' started by brennancaldwell, Jul 27, 2020.

  1. brennancaldwell

    brennancaldwell

    Joined:
    Jul 26, 2020
    Posts:
    2
    I'm having a problem where no matter what kind of object I type in code, Visual Studio won't recognize the reference on a later line. For instance, right now I have

    Code (CSharp):
    1. int[] listofsetups;
    2. listofsetups = new int[4];
    and when I put my mouse over the second listofsetups, it tells me "The name 'listofsetups' does not exist in the current context." If I declare and create the array in one line, trying to access listofsetups on a later line will give me the same error message. The editor is doing this for every kind of object I try to create, so I'm suspecting it's something to do with the editor although I restarted and updated it and that didn't fix anything. Any help solving this problem would be much appreciated.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Can you share more of the code here? Is this inside a method? Directly inside of a class? If it's directly in a class you have to declare and initialize in the same line, and generally can't reference other variables. You need to put any code that references the variables into methods.
     
  3. brennancaldwell

    brennancaldwell

    Joined:
    Jul 26, 2020
    Posts:
    2
    It was directly inside of the class, not in a method. I moved the second line to a method and that fixed it. Dumb newbie mistake on my part. Thanks so much for your help!
     
    PraetorBlue likes this.