0
//Excerpt from the script where i get the component

 public GameObject Tile_Manager;

 List<GameObject> TL = Tile_Manager.GetComponent<TM>().TilesL;

// it's the Tile_Manager bit that's underlined in red if that's any help

//How i defined the list in my other script  (TM)

public List<GameObject> TilesL = new List<GameObject>();
Matt Burland
  • 44,552
  • 18
  • 99
  • 171
  • It means what it says. You can't access `Tile_Manager` because it's an instance field and you don't have an instance. You should probably initialize `TL` in a constructor. – Matt Burland Nov 28 '16 at 17:50

2 Answers2

3

See https://stackoverflow.com/a/14439262/1193647 for an explanation of why this can't work. Basically, you can't reference the field you made, Tile_Manager when initializing another field in that class, since the compiler can choose to run them in a different order when first initializing the class.

Also, right now Tile_Manager is null, so even if this compiled, you'd get a NullReferenceException when it ran. To solve this, you should probably add a constructor for your class where you initialize Tile_Manager, and then in the following line initialize TL

Community
  • 1
  • 1
Kolichikov
  • 2,944
  • 31
  • 46
0

Perhaps,you can try swap the second line with third line...

  • Read carefully. 2nd line is in one script, 3rd in another. How to swap them? – MarianD Nov 28 '16 at 18:02
  • public static List TilesL = new List(); //Sorry.This time I read carefully.It's right?Use static to define this variable,and access it in another script. – ThinkExplorer Nov 28 '16 at 18:10