I have a very beginning C# question. Suppose I have a class called GameObject, and I want to create an array of GameObject entities. I could think of writing code like:
GameObject[] houses = new GameObject[200];
The compiler complains (assuming because of invalid syntax). Since this is XNA development, I load my texture in the LoadContent() method as follows:
houses[0].Model = Content.Load<Model>("Models\\Building_01 Windowed");
where houses[0] should be a GameObject and can be loaded like this, but the compiler throws this error:
"Use the "new" keyword to create an object instance"
"Check to determine if the object is null before calling the method"
There must be something wrong with my initialization.
The issue here is that you’ve initialized your array, but not its elements; they are all null. So if you try to reference
houses[0], it will benull.Here’s a great little helper method you could write for yourself:
Then you could initialize your
housesarray as: