- Home /
Problem with Singleton and NullReferenceException?
EDIT: Problem solved. It actually was super simple. Let this be a lesson to you: When you get confused, get up, take a walk and then come back and re-look at your code. It could save you hours. In this case, it was here:
public Block reinforcedIron = new Block(1, "Reinforced Iron");
This gets executed before Awake(). I had to just put the definition into Start(). So easy!**
Hello there.
I've been working on a game similar to Terraria where you can place "blocks" and break them again. For this I have been trying to make a class for the blocks to easily add new blocks and get information from them later on in the game. This is my "Block" class so far: using UnityEngine; using System.Collections; using System.Collections.Generic;
public class Block {
public int ID;
public string Name;
public Transform Prefab;
public Block(int sentID, string sentName) {
ID = sentID;
Name = sentName;
if(Control.instance.prefabs[ID] != null) {
Prefab = Control.instance.prefabs[ID];
} else {
Debug.LogError ("Warning! There is no prefab for block with ID " + ID);
}
}
}
I want to later on be able to make a new Block by doing "Block dirt = new Block(1, "Dirt");" for example to make a dirt block. Then the Block class will auto assign all the information for it (e.g. it will search an Array (seen in the next script) for the Transform corresponding to it's ID) and I can get the ID via dirt.ID, the name via dirt.Name and the prefab via dirt.Prefab).
This is my "Control" class: using UnityEngine; using System.Collections; using System.Collections.Generic;
public class Control : MonoBehaviour {
public static Control instance;
public static int[,] blocks = new int[999,999];
Vector2 mousePos;
public Block reinforcedIron = new Block(1, "Reinforced Iron");
public Transform[] prefabs = new Transform[999];
void Awake() {
instance = this;
}
void Start () {
Debug.Log (prefabs[0].name);
for(int x=0; x < blocks.GetLength(0); x++) {
for(int y=0; y < blocks.GetLength(1); y++) {
blocks[x,y] = -1;
}
}
}
void Update() {
mousePos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10));
}
}
It's a Singleton so that I can access the prefab array for prefabs from the Block class and assign the prefabs in the Editor. This way I can search a prefab by its ID in the Block class (see above for more info).
Now I am getting these two Errors when I start the game:
NullReferenceException: Object reference not set to an instance of an object
Block..ctor (Int32 sentID, System.String sentName) (at assets/Block.cs:14)
Control..ctor ()
IndexOutOfRangeException: Array index is out of range.
Block..ctor (Int32 sentID, System.String sentName) (at assets/Block.cs:14)
Control..ctor ()
Why? What am I doing wrong? For those interested, the "prefab" in my Editor is empty, I however to test added 1 prefab to it (made size to 1 and added a prefab) to see if it can store things. Why is the Block class throwing an IndexOutOfRangeException when I haven't even accessed anything through it yet? And why is there an NullReferenceException when I am using a Singleton?
Thank you, Sincerely, aleichert
Please post an answer ins$$anonymous$$d of placing an answer in your question.
Your answer
Follow this Question
Related Questions
Instantiating gameobjects in an array / class problem | NullReferenceException: 0 Answers
Why Again Arrays In ExecuteInEditMode() Gives NullReferenceException Errors?? 1 Answer
"Null Reference Error" when using a custom class as an array 1 Answer
Pushing GameObject into JS Array returns NullReferenceException 1 Answer