- Home /
Object Reference Not Set
I realize this question has been asked many times, but I looked through the other questions and couldn't find my issue. Here's my code:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class DisableMoveWhileFlying : MonoBehaviour {
private Move obj;
private List<GameObject> Colliding;
void Start(){
obj = GetComponent<Move>();
}
void OnCollisionExit2D(Collision2D Other){
obj.enabled = false;
Colliding.Remove(Other.gameObject);
Debug.Log(Other.gameObject);
}
void OnCollisionEnter2D(Collision2D Other){
obj.enabled = true;
Colliding.Add(Other.gameObject);
}
}
This gives a Null Reference Exception, and I can't find why. The lines that toggle obj.enabled work fine, but the Debug.Log() line does not.
Thank you.
try debugging, or put more Debug.Logs. you should make sure, the Other GameObject gets into the Colliding List. $$anonymous$$aybe you want to Remove it, but it has never been there.
What is the name of the Game object the $$anonymous$$ove script is attached to?
Thank you everyone, I fixed it. private List Colliding does not actually initialize a list. private List Colliding = new List does. Thanks everyone for your time.
Answer by OmniKeith · Apr 19, 2015 at 08:34 PM
you need to disable the object after you call on it. you have the object disabled then when you try to call on the object, it looks for the object and because it is disabled it can't find it.
Answer by galshal · Apr 19, 2015 at 08:04 PM
maybe you should try
void Start(){
obj = gameObject.GetComponent<Move>();
}
i had the same problem i think that solved it