Hi guys, I have an issue with NullReferenceException, I just can’t figure it out. I have 2 scripts on different objects, WorldOrder and SpawnGround. SpawnGround is supposed to get the world order from WorldOrder and then spawn the ground around the player. Here’s SpawnGround, the error line is in OnTriggerEnter2D and marked with a comment:
public class SpawnGroundFront : MonoBehaviour {
public GameObject player;
int groundIndex;
int loadDist;
List<string> worldOrder;
void Start(){
player = GameObject.FindWithTag("Player");
try{
loadDist = player.GetComponent<WorldOrder>().loadDist;
groundIndex = player.GetComponent<WorldOrder>().groundIndex;
worldOrder = player.GetComponent<WorldOrder>().worldOrder;
}
catch(NullReferenceException ex){
Debug.Log("Player not found!");
}
}
void OnTriggerEnter2D(Collider2D other){
if(player != null){
if(other.gameObject == player){
//get the stack of world names
float vel = other.GetComponent<Rigidbody2D>().velocity.x;
//player is moving forward
if(vel > 0){
//there is no ground data to load
if(worldOrder[groundIndex + loadDist + 1] == null){ //THIS LINE GIVES THE ERROR
SpawnForwardNew();
PushBackwardStack();
//groundIndex++;
}
//there is some ground data to load
else{
SpawnForwardLoad();
}
}
else if(vel < 0){
PushForwardStack();
}
}
}
else{
Debug.Log("No player object!");
}
}
Here is WorldOrder:
public class WorldOrder : MonoBehaviour {
//other methods treat this as a stack where push adds a value to the front or back, same with pop
public List<string> worldOrder;
public GameObject[] groundObj;
//public GameObject[] platformObj;
public int loadDist = 2;
public int groundIndex;
void Awake(){
if(GameObject.FindWithTag("Ground") == null){
SpawnInitial();
groundIndex = loadDist;
}
}
void GetWorldOrder(){
GameObject curr = GameObject.FindWithTag("Ground");
}