Ryan
Ryan

Reputation: 23

Unity - Issues with constructor returning Null

When I refer to tiles they are sprites in Unity with characteristics.

Context: I am trying to add a Tile(make with a constructor in Class Tile) to a list. The Tile variable basically consists of health, sprite, id, etc of that type of tile. All of those variables inside that Tile object are not Null and have some kind of value in them. My problem is that when I put a watch on the variables when running it they are just in general null. The only guess I have to why this happens is because the variable called base inside the Tile object is also Null but I have no idea what so ever on how to fix this problem.

This is the class called Tile

    public int ID { get; set; }
public string Name { get; set; }
public Sprite Image { get; set; }
public int Durability { get; set; }
public bool Destructible { get; set; }
public static List<Tile> Tilelist; //Where I plan on storing all the tiles for later use


public Tile()
{
    List<Tile> TileList = new List<Tile>();
}

public Tile(int id, string name, int durability, bool destructible, Sprite image)
    : this()
{

    ID = id;
    Name = name;
    Image = image;
    Durability = durability;
    Destructible = destructible;
}

This is the class called TileAssign where I create all the tiles and their attributes and then add them to Class Tile's List

public Tile[] TileSprite(Tile[]Tiles)
{
    int TilesNum = Tiles.Length;

    for (int i = TilesNum - 1; i > 0; i--) 
    {
        Tiles[i].Image = Resources.Load<Sprite>("TileSprites/" + Tiles[i].Name); 
    } //Running through my assets in Unity and finding sprites to match up with tile


    return Tiles;
}



public void PopulateTiles()
{


    Tile Unnamed = new Tile(0, "Unnamed", 0, false, null);
    Tile SpaceStationFloor1 = new Tile(1, "SpaceStationFloor1", 50, true, null); 
    Tile SpaceStationWall1 = new Tile(2, "SpaceStationWall1", 100, true, null);
    Tile Thrusters = new Tile(3, "Thrusters", 75, true, null);

    Tile[] TilesInitialized = new Tile[] {
        Unnamed,
        SpaceStationFloor1,
        SpaceStationWall1,
        Thrusters
    };  //Creating Tiles here ^^^^
    //Plugging in my list to get corresponding sprites \/
    TilesInitialized = TileSprite(TilesInitialized); 
    AddToList(TilesInitialized); //Sending list to fucntion to add them to tile's list

}

private static void AddToList(Tile[] TilesInitialized)
{

    for (int i = TilesInitialized.Length - 1; i >= 0; i--)
    {
        Tile Newtile = TilesInitialized[i]; 
        Tile.Tilelist.Add(Newtile); //Where I run into my issue
    }
}
private void Start()
{
    PopulateTiles();        
    Instantiate(Resources.Load("Tile1"), new Vector3(0f,0f,0f), Quaternion.identity);
}

Look At bottom Left, the TilesInitialized Array and the individual Tile objects inside are their along with their characteristics and such, although a little explanation of what exactly the "base" part of it is would help and why it's null

Upvotes: 0

Views: 623

Answers (1)

Ilian
Ilian

Reputation: 5355

You are initializing a temporary variable here:

public Tile()
{
    List<Tile> TileList = new List<Tile>();
}

It should just be:

public Tile()
{
    TileList = new List<Tile>();
}

Should TileList be really static? If so, don't initialize it in a constructor. You will erase TileList each time you create a new Tile.

Just declare it as:

public static List<Tile> TileList = new List<Tile>();

public Tile()
{
}

Upvotes: 3

Related Questions