I have the following code. When I try to change the position I get an error that says “NullReferenceException: Object reference not set to an instance of an object”. I don’t understand why. My second question is why my grid list wont show up in my editors in unity. I have a picture of my editor.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class myGrid : MonoBehaviour
{
private int width;
private int height;
private float size;
public GameObject defualtSprite;
[SerializeField]
public GameObject [,] grid;
private void Start()
{
myGrid grid = new myGrid(4, 2, 10);
}
public myGrid(int width, int height, float size)
{
this.width = width;
this.height = height;
this.size = size;
grid = new GameObject[width, height];
for(int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
// My first method
//Transform trans = defualtSprite.GetComponent<Transform>();
//trans.position = new Vector3(x * size, y * size, 0);
// My second method
defualtSprite.transform.position = new Vector3(x * size, y * size, 0);
grid[x, y] = defualtSprite;
}
}
}
}
