【问题标题】:Unity - Issues with constructor returning NullUnity - 构造函数返回 Null 的问题
【发布时间】:2018-10-27 09:03:59
【问题描述】:

当我提到瓷砖时,它们是 Unity 中具有特征的精灵。

上下文:我正在尝试将 Tile(使用 Class Tile 中的构造函数制作)添加到列表中。 Tile 变量基本上由该类型瓷砖的健康、精灵、id 等组成。该 Tile 对象中的所有这些变量都不是 Null 并且在其中具有某种值。我的问题是,当我在运行变量时监视变量时,它们通常为空。我对为什么会发生这种情况的唯一猜测是因为 Tile 对象中名为 base 的变量也是 Null,但我不知道如何解决这个问题。

这是名为 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;
}

这是一个名为 TileAssign 的类,我在其中创建所有图块及其属性,然后将它们添加到类图块的列表中

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);
}

【问题讨论】:

    标签: c# unity3d constructor


    【解决方案1】:

    您正在这里初始化一个临时变量:

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

    应该是:

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

    TileList 真的应该是static 吗?如果是这样,请不要在构造函数中初始化它。每次创建新的Tile 时,您都会删除TileList

    只要声明为:

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

    【讨论】:

    • 谢谢,这解决了我的问题。最后一个问题,我的 Tile 变量的“基础”部分到底是什么?为什么它为空?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-03
    • 2012-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多