NullReferenceException: Object reference not set to an instance of an object, again
I am a beginner to Unity C#, but not C# itself. I have seen this topic several times here but none seem to answer my questions, PLEASE help! This class is the location of the error.
using UnityEngine;
using System.Collections;
public class WorldGen : MonoBehaviour {
private BlockManager blockManager;
private int width = 64;
private int height = 128;
private Block[,] blocks;
public GameObject GameManager;
private void Start()
{
blockManager = GameObject.Find("GameManager").GetComponent<BlockManager>();
blocks = new Block[width, height];
GenerateBlocks();
SpawnBlocks();
}
private void GenerateBlocks()
{
for (int x = 0; x < width; x++)
{
for(int y = 0; y < height; y++)
{
blocks[x, y] = blockManager.FindBlock(2);
}
}
}
private void SpawnBlocks()
{
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
GameObject blockGO = new GameObject();
SpriteRenderer sr = blockGO.AddComponent<SpriteRenderer>();
sr.sprite = blocks[x, y].sprite;
blockGO.name = blocks[x, y].display_name;
blockGO.transform.position = new Vector3(x, y);
}
}
}
}
Originally I had followed a tutorial exactly as far as I could tell, but they did not get this error, and I did. Tutorial: https://www.youtube.com/watch?v=wqivozn6Sgc&list=PLyJRgNGLgSoW7iGxKI5ZAL0IfGrlqMeG9∈dex=3
Answer by Glurth · Nov 01, 2015 at 04:35 PM
We will need to know which line generates the error, to help you find it, but here is some info that will help you find this one yourself, and others in the future.
This error means that your code is attempting to access a member variable, or function, for an object that has not been created yet (is still null).
So, for example: Lets say this function calls FAILS to find the object you are looking for:
GameObject.Find("GameManager")
If it fails to find the object, it will return a NULL value. So, when you try to access the GetComponent function like so:
GameObject.Find("GameManager").GetComponent<BlockManager>();
it would generate a null reference exception, because we are in effect saying:
null.GetComponent<BlockManager>();
which doesn't make much sense.
The safer way to do this would be like so:
GameObject foundObject = GameObject.Find("GameManager");
if(foundObject!=null)
foundObject.GetComponent<BlockManager>();
else
Debug.LogError("No block manager found in scene");
Thanks! So I added what you suggested, and it is not the source of the error, this is:
private void GenerateBlocks()
{
for (int x = 0; x < width; x++)
{
for(int y = 0; y < height; y++)
{
blocks[x, y] = block$$anonymous$$anager.FindBlock(2);
}
}
}
Or more specifically, blocks[x, y] = block$$anonymous$$anager.FindBlock(2);
The full error message, in case you want it, is:
NullReferenceException: Object reference not set to an instance of an object
WorldGen.GenerateBlocks () (at Assets/BurnseyEntertainment/Scripts/WorldGen.cs:39)
WorldGen.Start () (at Assets/BurnseyEntertainment/Scripts/WorldGen.cs:29)
ok, so the error is saying that this line:
blocks[x, y] = block$$anonymous$$anager.FindBlock(2);
when running, is co$$anonymous$$g up as, either:
blocks[x, y] = null.FindBlock(2);
or
null[x, y] = block$$anonymous$$anager.FindBlock(2);
or BOTH.
null[x, y] = null.FindBlock(2);
Answer by Granite Bear · Nov 01, 2015 at 07:02 PM
Answer Found! The GameObject "Manager" was missing the script required for the code to run. Once it was replaced... It worked! Wonderfully! Check your scripts everyone!