- Home /
Question by
stfj · Jan 18, 2014 at 09:35 PM ·
objective-c
Object Reference not set... yet I can access it's variables?
I've been getting an Object Reference not set to Instance of variable on the last line of this code:
private List<EventScript> events;
GameObject newEventObj = Instantiate (Resources.Load ("Event"), myTransform.position, Quaternion.identity) as GameObject;
EventScript newEvent = newEventObj.GetComponent<EventScript>();
newEvent.initial = initial;
Debug.Log (newEvent.initial);
events.Add (newEvent);
but what's weird, is that the Debug.Log fires off just fine, and gets the appropriate variable. Even the setting of that variable works (the gameObject shows it, so i can see it change).
I'm so confused as to how the object suddenly becomes dereferenced when I try to add it to a list
Thanks for any help!
Comment
Best Answer
Answer by robertbu · Jan 18, 2014 at 09:37 PM
It's complaining about your use of 'events' not about 'newEvent'. You declare but don't initialize 'events': Change line 1 to:
private List<EventScript> events = new List<EventScript>();