- Home /
NullReferenceException when getting value from different script
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");
}
Answer by GregoryNeal · Apr 25, 2015 at 10:37 AM
The object that holds WorldOrder is a child of my player so I should have used GetComponentInChildren instead of GetComponent.
Answer by Tomer-Barkan · Apr 25, 2015 at 10:32 AM
Seems like you never assign a value to worldOrder
in the WorldOrder class, so it's null. Then you try to acess one of it's members.
Try changing the line to
public List<string> worldOrder = new List<string>();
The only other thing I can think about is that OnTriggerEnter2D is called before Start(), though I can't think of why this happens. You can check this by adding logs in each method and see which one is called first.
Hey man thanks for the reply, I did figure it out just now though. The object that holds WorldOrder is a child of my player so I should have used GetComponentInChildren ins$$anonymous$$d of GetComponent.
Thats weird, I thought of that scenario but then:
worldOrder = player.GetComponent<WorldOrder>().worldOrder;
would throw an exception if the GetComponent returns null and you try to access the worldOrder
variable...
Your answer
Follow this Question
Related Questions
Random: NullReferenceException: Object reference not set to an instance of an object 2 Answers
ScriptableObject reference resetted after a method is finished. 2 Answers
HELP!!! This NullReferenceException won't go away! :( 1 Answer
How can I access another script in my prefab to set a variable after my network player setup runs? 1 Answer
Trigger reference problem 1 Answer