- Home /
NullReferenceException on Public Variable only Outside the Class
It doesn't really make sense to me that inside the class I have set up the variable called allTiles and gave it multiple values but every time i refer to allTiles outside the class i get a NullReference Error as if all the values are there but are set to null D:
The TileHill is a sub-class of Tile and they both have constructors to set numUnits to 0.
public class TileManager : MonoBehaviour {
public const int TILES_X = 12;
public const int TILES_Y = 6;
public Texture[] terran = new Texture[2];
public Tile[,] allTiles = new Tile[TILES_X, TILES_Y];
void Start () {
for(int i = 0; i < TILES_Y; i++) {
for(int j = 0; j < TILES_X; j++) {
if(Random.Range(0.0f, 1.0f) < 0.5f){
allTiles[j, i] = new Tile();
}
else{
allTiles[j, i] = new TileHill();
}
}
}
}
}
public class GameScreen : MonoBehaviour {
TileManager tileManager = new TileManager();
GameManager gameManager = new GameManager();
void OnGUI () {
int BTN_SIZE_X = Screen.width/12;
int BTN_SIZE_Y = Screen.height/8;
float groupSizeX = BTN_SIZE_X * TileManager.TILES_X;
float groupSizeY = BTN_SIZE_Y * TileManager.TILES_Y;
GUI.BeginGroup(new Rect((Screen.width - groupSizeX) / 2f, 0, groupSizeX, groupSizeY));
for(int i = 0; i < TileManager.TILES_X; i++) {
for(int j = 0; j < TileManager.TILES_Y; j++) {
if(GUI.Button(new Rect(BTN_SIZE_X*i, BTN_SIZE_Y*j, BTN_SIZE_X, BTN_SIZE_Y), "" + tileManager.allTiles[0,0].numUnits)){
gameManager.setSelection(i, j);
}
}
}
GUI.EndGroup();
}
}
First of all, unity editor will give you a warning that you cant instantiate a class which is child class of monobehaviour. You did that in the line:- Tile$$anonymous$$anager tile$$anonymous$$anager = new Tile$$anonymous$$anager();
You cant instantiate the whole monobehaviour again
Answer by troien · Apr 09, 2015 at 11:25 AM
First of all, sinse TileManager is a MonoBehaviour, you can't instantiate it using the 'new' keyword (Unity should display a warning in the console notifying you about this I believe)
That leaves me to what is most likely your problem, GameScreen.tileManager is not set. sinse this is a private variable and you can't instantiate it using the new keyword, so tileManager is null ;) Simplest thing would be to make it a public variable and assign it in the inspector. Or use another way to find the TileManager and assign it in the Awake/Start (GameObject.Find/GetComponent/etc.)
Same thing counts for the GameScreen.gameManager if this is a MonoBehaviour btw.
Hmm so ins$$anonymous$$d of Tile$$anonymous$$anager tile$$anonymous$$anager = new Tile$$anonymous$$anager(); How would I call it ? Tile$$anonymous$$anager tile$$anonymous$$anager = Tile$$anonymous$$anager();?? or something like that??
No, not like that. Create two different scripts for both classes and attach both of them on same gameobject get reference using GetComponent method.
No, not like that. Create two different scripts for both classes and attach both of them on same gameobject get reference using GetComponent method.
Well, how you would call it depends on how you intend to use it. Sinse there are many ways.
One of them would be to make Tile$$anonymous$$anager a class that does not extend $$anonymous$$onoBehaviour (Note that when you do that, Start,Awake and Update won't work as they are callbacks that are called by $$anonymous$$onoBehaviour. It then is a plain old C# class, which would mean you have to replace Start with a constructor). Andvantage of this is that you can call that constructor using the 'new' keyword like you do now. See also this question.
If you want it to be a $$anonymous$$onoBehaviour, here are a few options:
public class GameScreen : $$anonymous$$onoBehaviour
{
// This way you should set them in the inspector of GameScreen
public Tile$$anonymous$$anager tile$$anonymous$$anager;
public Game$$anonymous$$anager game$$anonymous$$anager;
}
public class GameScreen : $$anonymous$$onoBehaviour
{
// If you dont want to set it in the inspector, and simply want to create a new Tile$$anonymous$$anager and Game$$anonymous$$anager for every GameScreen you have
Tile$$anonymous$$anager tile$$anonymous$$anager;
Game$$anonymous$$anager game$$anonymous$$anager;
private void Awake()
{
tile$$anonymous$$anager = gameObject.AddComponent<Tile$$anonymous$$anager>();
game$$anonymous$$anager = gameObject.AddComponent<Game$$anonymous$$anager>();
}
}
[RequireComponent(typeof(Tile$$anonymous$$anager), typeof(Game$$anonymous$$anager))]
public class GameScreen : $$anonymous$$onoBehaviour
{
// This one is the same as the previous, but the difference is that when you now add a GameScreen to anything in the scene, Tile$$anonymous$$anager and Game$$anonymous$$anager will automatically be added
Tile$$anonymous$$anager tile$$anonymous$$anager;
Game$$anonymous$$anager game$$anonymous$$anager;
private void Awake()
{
tile$$anonymous$$anager = GetComponent<Tile$$anonymous$$anager>();
game$$anonymous$$anager = GetComponent<Game$$anonymous$$anager>();
}
}
For the last example, see also: RequireComponent
Answer by Mewada-Neil · Apr 09, 2015 at 11:40 AM
As I mentioned in the comment, Unity prevents the GameScreen class from creating an instance of the TileManager class.
It would also happen if your GameManager class is also derived from MonoBehaviour class.
Thus the tileManager references will remain null.
Your answer
![](https://koobas.hobune.stream/wayback/20220613192606im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
[Solved] Actually totally explainable NRE with 2D array 0 Answers
Null exception for public variable 2 Answers
How to add a gameobject to a null gameobject 1 Answer
Why am I getting a NullReferenceException while trying to set a gameobjects animation frame? 0 Answers
NullReferenceException .. problem with Camera.main.transform 2 Answers