Object reference not set to an instance of an object + array of positions
Hi! I know what the general gist of this error means but am kinda confused about how to fix it in my situation. This is my manager script for a mahjong type game, and I want to spawn tiles starting from an initial point far left and then moving towards the right, so I end up with a row. I tried doing this by making an array where every next element was translated a little bit to the right, and would subsequently be the next spawn point. The null reference error occurs in Line 17. Also is there a better way of doing this? And what am I doing wrong? Thank you so so much in advance!!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TileManager : MonoBehaviour {
public GameObject tile;
public Transform initialSpawnPoint;
Transform[] tileSpawn;
Vector3 initialSpawnPointPos;
Transform[] Awake(){
initialSpawnPointPos = initialSpawnPoint.position;
for (int i = 0; i < 13; i++) {
tileSpawn [i].position = new Vector3(4*i, initialSpawnPointPos.y, initialSpawnPointPos.z);
}
return tileSpawn;
}
void Start(){
for (int i = 0; i < 13; i++) {
TileSpawn (tileSpawn [i]);
}
Debug.Log (tileSpawn [0]);
}
void TileSpawn(Transform spawnPoint){
Instantiate (tile, spawnPoint.position, spawnPoint.rotation);
}
}
I've created a script that does what i think it is you want to do.
$$anonymous$$ess with the numbers and such and you should get the hang of it, if you need any other help feel free to email/pm me.
Answer by Insurikai_ · Jul 26, 2017 at 03:50 PM
The problem is that the Awake Function is called 1st and in the for loop you have you are trying to access a tileSpawn that doesn't exist yet.