- Home /
Access System.Serializable class from script?
I'm trying to declare by script the following struct:
[System.Serializable] public class GoogleMapMarker { public enum GoogleMapMarkerSize { Tiny, Small, Mid } public GoogleMapMarkerSize size; public GoogleMapColor color; public string label; public GoogleMapLocation[] locations;
}
using
GoogleMapMarker[] markers = new GoogleMapMarker[2];
markers[0].color = GoogleMapColor.red;
markers[1].color = GoogleMapColor.blue;
But, I always end up with "NullReferenceException: Object reference not set to an instance of an object" on the line where markers[0] starts...
I had thought I defined it to be an array of size 2, so I am not sure why it is saying null object? Is System.Serializable not accessible from script? Is there another way to do this?
Answer by flaviusxvii · Feb 17, 2013 at 01:10 AM
Declaring an array of objects doesn't actually make any objects. It's a container for objects. You still have to initialize each one with markers[n] = new GoogleMapMarker();
Your answer
![](https://koobas.hobune.stream/wayback/20220613092829im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multidimensional GameObject Arrays 1 Answer
Null reference when accessing GameObject in the Array(C#) 1 Answer
NullReferenceException: Object reference not set to an instance of an object 1 Answer
NullReference when accessing GameObject in array (C#) 1 Answer
Split() into a fixed length array? 1 Answer