0

This is all in Unity using Monodevelop

I declare my array like this

Tile[] Tiles = new Tile[7];

Then I try and loop through and set the elements (though this itself was an attempt to solve this problem) like this

for (int i = 0; i < Tiles.Length; i++) 
{
    Tiles[i] = new Tile();
}

However the collection is still filled with 7 null objects. I must be missing something simple. I would have thought the initial line of code would suffice.

Entire example:

using UnityEngine;
using System.Collections;

public class Foo : MonoBehaviour
{
    Tile[] Tiles = new Tile[7];

    // Use this for initialization
    void Start ( ) 
    {
        for (int i = 0; i < Tiles.Length; i++) 
        {
            Tiles[i] = new Tile();
        }
    }
}

This is the Tile class

using UnityEngine;
using System.Collections;

public class Tile
{
    public Tile[] nonAdjacentTiles = new Tile[6];
    public Transform _mesh;
}
Dollarslice
  • 9,917
  • 22
  • 59
  • 87
  • Can you post a complete example that demonstrates the problem that we can run? Your code so far implies that the issue you are getting is not possible as the items in the array are successfully initialised. – Adam Houldsworth Sep 10 '14 at 10:22
  • Please show a complete example. What you show should not result in the behavior you describe. – Sergey Kalinichenko Sep 10 '14 at 10:23
  • That is the entire example – Dollarslice Sep 10 '14 at 10:24
  • 1
    @SirYakalot An entire example is a console application that someone else can run to reproduce your issue. Were I to "fill in the gaps" to get your code running, it would work fine. So clearly there is something else going on such as you are looking at a different object reference perhaps. – Adam Houldsworth Sep 10 '14 at 10:25
  • OK, there you go. so it is within unity, maybe that#s the problem. I've re-tagged as appropriate – Dollarslice Sep 10 '14 at 10:28
  • 1
    @SirYakalot Have you confirmed that the `Start()` method is called prior to you reviewing the state of `Tiles`? The [documentation](http://docs.unity3d.com/ScriptReference/MonoBehaviour.html) for it states it is called just before any of the `Update`methods are called. – Adam Houldsworth Sep 10 '14 at 10:29
  • Yes, I was inspecting the collection after the loop ran by placing a breakpoint. – Dollarslice Sep 10 '14 at 10:30
  • @SirYakalot Something smells fishy to me. That code works fine, so you can be assured that you are initialising the array elements correctly. My suspicion is either you are debugging at a point too early, or Unity has some strange involvement in this mess. – Adam Houldsworth Sep 10 '14 at 10:31
  • It seems that 'new Tile()' is returning null. why would that be? – Dollarslice Sep 10 '14 at 10:36
  • @SirYakalot Not sure, the only thing I know that hijacks the `new` keyword is the old .NET Remoting infrastructure. Is `Tile` a Unity class? This is why things should not hijack `new`, confusion all around. – Adam Houldsworth Sep 10 '14 at 10:40
  • @SirYakalot: what debugger are you using? Both MonoDevelop and VS (with extension) will allow you to set a breakpoint in Unity code but the inspection of variables is often a little broken. I'd suggest you use `Debug.Log` to output the content of the array (do this inside your loop to check every element, even), just to be sure. – Dan Puzey Sep 10 '14 at 10:45
  • I thought the problem was that I was trying to instantiate a monobehaviour, which is a mistake, but I changed the class and the problem persists. I'll continue to investigate and post back when I find something. – Dollarslice Sep 10 '14 at 11:12
  • ARRRGGHHHH!!! I have NO idea why this is happening!!! – Dollarslice Sep 10 '14 at 11:39
  • Ah OK, turns out SOMETHING whether it's monodevelop or the version of C# that it uses does not support default constructors. I'll update the question to make it useful – Dollarslice Sep 10 '14 at 11:43

3 Answers3

0

For some reason a constructor is needed in the Tile class, I'm not sure why exactly, but using Unity and Monodevelop it seems default constructors are not supported.

Dollarslice
  • 9,917
  • 22
  • 59
  • 87
0

Siryakalot as you say, you need a constructor in the Tile class because you can't instanciate an object of Type Tyle like this :

Tiles[i] = new Tile();

without having a constructore Tile() in the Tile class

good luck :)

karim
  • 167
  • 1
  • 1
  • 6
-1

Nope, the default for an array of classes is null. So you need to loop through and fill the array afterwards.

I like the solution posted here for a clean population of your array. https://stackoverflow.com/a/4839502/4018288

Community
  • 1
  • 1
  • Welcome to Stack Overflow! Whilst this may theoretically answer the question, [it would be preferable](http://meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Vogel612 Sep 10 '14 at 10:29
  • @Vogel612 Unfortunately, this gets no where near answering the question... and is at best a comment. – Adam Houldsworth Sep 10 '14 at 10:30
  • @AdamHouldsworth hmm... point taken. Alas, the answer probably deserves a NAA flag.. I just downvoted and auto-commented about the quality of the answer, because it was immediately obvious to me that this point needs it the most. I didn't even check for correctness ... – Vogel612 Sep 10 '14 at 10:33