简体   繁体   English

Tilemaps.Tilemap.SetTile() 据说不存在(Unity)

[英]Tilemaps.Tilemap.SetTile() supposedly doesn't exist(Unity)

Relevant piece of code:相关代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;

public class Generation : MonoBehaviour
{
    private int[,] CavePoints;

    public RuleTile Stone;

    public Camera Camera;

    private int CurrentMapLength = 0;

    private void Awake()
    {
        SewerGeneration(5);
    }

    void Start()
    {
        PlaceGrid();
    }

    public void SewerGeneration
    {
        *Code*
    }

    //Place Tiles on grid

    private void PlaceGrid()
    {
        for (int x = 0; x < CurrentMapLength + 1; x++) //width of map
        {
            for (int y = 0; y < 56; y++) //height of map
            {
                try
                {
                    if (CavePoints[x, y] == 1)    //CHANGE TO CavePoints
                    {
                        Tilemap.SetTile(new Vector3Int(x, y, 0), Stone);
                    }
                }catch (System.Exception e)
                {

                }
            }
        }
    }
}

However, the Tilemap.SetTile() under the function PlaceGrid() returns:但是,function PlaceGrid() 下的PlaceGrid()返回:

Assets/Scripts/Generation.cs(124,25): error CS0120: An object reference is required for the non-static field, method, or property 'Tilemap.SetTile(Vector3Int, TileBase)'

I then added Tilemaps.然后我添加了 Tilemaps。 in front of the SetTile, and the code returned this:在 SetTile 前面,代码返回如下:

Assets/Scripts/Generation.cs(124,25): error CS0103: The name 'Tilemaps' does not exist in the current context

Adding UnityEngine and removing Tilemap also yielded similar results.添加 UnityEngine 并删除 Tilemap 也产生了类似的结果。

Help appreciated.帮助表示赞赏。 Thanks.谢谢。

as the error tells you Tilemap.SetTile is not a static method.正如错误告诉您Tilemap.SetTile不是static方法。

You cannot simply call it via the type like你不能简单地通过像这样的类型来调用它

Tilemap.SetTile(...);

you rather need a reference to an instance of Tilemap eg like您宁愿需要引用Tilemap的实例,例如

[SerializeField] private Tilemap myTilemap;

assign this via the Inspector in Unity by drag&drop the according object/component into that field and then do通过将相应的对象/组件拖放到该字段中,通过 Unity 中的 Inspector 分配它,然后执行

myTilemap.SetTile(...);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM