[Begginer] [Solved] How to solve a Null Reference Exception
This is the error message I am getting.. I am a beginner of coding but it was actually a tutorial on youtube I followed and I can't see what error I made even though I replayed the video many times. Thanks for any help :D
NullReferenceException: Object reference not set to an instance of an object LevelManager.PlaceTile (System.String tileType, Int32 x, Int32 y, Vector3 worldStart) (at Assets/Scripts/LevelManager.cs:60) LevelManager.Createlevel () (at Assets/Scripts/LevelManager.cs:49) LevelManager.Start () (at Assets/Scripts/LevelManager.cs:23)
This is my script
using System.Collections; using System.Collections.Generic; using UnityEngine; using System;
public class LevelManager : MonoBehaviour {
[SerializeField]
private GameObject[] tilePrefabs;
[SerializeField]
private CameraMovement cameraMovement;
public float TileSize
{
get { return tilePrefabs[0].GetComponent<SpriteRenderer> ().sprite.bounds.size.x; }
}
// Use this for initialization
void Start ()
{
Createlevel();
}
// Update is called once per frame
void Update ()
{
}
private void Createlevel ()
{
string[] mapData = ReadLevelText ();
int mapX = mapData[0].ToCharArray().Length;
int mapY = mapData.Length;
Vector3 maxTile = Vector3.zero;
Vector3 worldStart = Camera.main.ScreenToWorldPoint(new Vector3 (0, Screen.height));
for(int y = 0; y < mapY; y++)
{
char[] newTiles = mapData [y].ToCharArray();
for(int x = 0; x < mapX; x++)
{
maxTile = PlaceTile (newTiles[x].ToString(),x,y,worldStart);
}
}
cameraMovement.SetLimits (new Vector3(maxTile.x + TileSize, maxTile.y - TileSize));
}
public Vector3 PlaceTile(string tileType, int x, int y, Vector3 worldStart)
{
int tileIndex = int.Parse (tileType);
TileScript newTile = Instantiate (tilePrefabs[tileIndex]).GetComponent<TileScript>();
The following line is where it says the error is
newTile.Setup(new Point(x,y), new Vector3 (worldStart.x + (TileSize * x), worldStart.y - (TileSize * y), 0));
return newTile.transform.position;
}
private string[] ReadLevelText()
{
TextAsset bindData = Resources.Load ("Level") as TextAsset;
string data = bindData.text.Replace(Environment.NewLine,string.Empty);
return data.Split('-');
}
}
Dang I was following the same tutorial and ran into the same problem. Was hoping you'd have found an answer.
Answer by tormentoarmagedoom · Apr 17, 2018 at 08:19 AM
Goo day guys.
This error, is the most common error at Unity.
It says : "A variable is not assigned when trying to execute the line of code number "number" at Script "scriptName"".
So.. You must discover 3 things:
1- What line of code is giving the error: Look at the error message, in our case is LevelManager script, at line 23 (CreateLevel()) and line 60 (which is the CreateLevel). (As you didnt pasted correctly all the script so i can't know what line is line 60
2- Discover what variable is not assigned (is still null) when trying to execute that line. To discover this, you can or debug the code while runnign, or put a Debug.Log message with all the variables just before the problematic line.
Debug.Log(variable);
To see in the console the value of all variables, and one of them will be null.
3- Now yo know what variable is returning null, by understanding your script, you must discover why is not assigned yet! (Maybe you need to assign it via inspector, or maybe by code...)
Thats the basic 3 stops to solve ALL null reference errors.
I helped, Accept the answer and close the question!
Bye!
Your answer
Follow this Question
Related Questions
Array of dictionaries: "Object reference not set to an instance of an object" 0 Answers
PLEASE HELP!! Object reference not set to an instance of an object at ChangeMasterVolume.Update 1 Answer
Getting an error: Assertion failed on expression: 'SUCCEEDED(hr)' 5 Answers
Error That Doesn't Affect My Game Is Spammed Every Frame. 1 Answer