- Home /
match 3 tutorial: unable to store gem position on the board. Whats wrong with the following code:
Error: NullReferenceException: Object reference not set to an instance of an object. Board.gemsSpawn (UnityEngine.Vector2Int pos, Gems gemsTospawn) (at Assets/my assets/scripts/Board.cs:45) Board.Setup () (at Assets/my assets/scripts/Board.cs:34) Board.Start () (at Assets/my assets/scripts/Board.cs:18)
code
public class Board : MonoBehaviour {
public int width;
public int height;
public GameObject BgtilesPrefab;
public Gems[] gems;
public Gems[,] allGems;
// Start is called before the first frame update
void Start()
{
Setup();//***** line 18
allGems = new Gems[width, height];
}
private void Setup() { for(int x = 0; x < width; x++) { for(int y= 0; y < height; y++) { Vector2 pos = new Vector2(x,y); GameObject bgtiles = Instantiate(BgtilesPrefab, pos, Quaternion.identity); bgtiles.transform.parent = transform; bgtiles.name = "Tile NO-" + x + "," + y;
int gemTouse = Random.Range(0, gems.Length);
gemsSpawn(new Vector2Int(x, y), gems[gemTouse]);//*****line43
}
}
}
private void gemsSpawn(Vector2Int pos, Gems gemsTospawn) {
Gems gem = Instantiate(gemsTospawn, new Vector3(pos.x,pos.y,0f), Quaternion.identity);
gem.transform.parent = transform;
gem.name = "Gem-" + pos.x + "," + pos.y;
allGems[pos.x, pos.y] = gem;//******line 45
gem.setupgems(pos,this);
}
}
Could you format your code a bit and also provide the Gems class as well? Like this there are just only guesses unfortunately
Your answer
![](https://koobas.hobune.stream/wayback/20220613045640im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
troubleshooting NullReferenceException 2 Answers
NullReferenceException 2 Answers
Material[] Object reference not set when instantiating 2 Answers
NullReference Exception when adding a class 2 Answers
NullReferenceException 1 Answer