You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity中Tilemap.SetTile()调用报错,求助解决CS0120与CS0103问题

Unity中Tilemap.SetTile()调用错误的解决办法

问题重现

调用Tilemap.SetTile()时触发以下错误:

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)'

尝试添加Tilemaps.前缀后出现新错误:

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

相关代码如下:

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(int value)
    {
        // 你的生成逻辑代码
    }

    //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)
                {

                }
            }
        }
    }
}

错误原因

Tilemap.SetTile()是非静态方法,必须通过Tilemap类的实例对象调用,直接用类名调用会导致编译器找不到对应实例,触发CS0120错误。而你尝试的Tilemaps前缀无意义,因为脚本中未定义该变量。

修复步骤

  • 在Generation脚本中添加Tilemap类型的公共引用变量:
    public Tilemap GroundTilemap; // 用于引用场景中的Tilemap组件
    
  • 回到Unity编辑器,选中挂载Generation脚本的游戏对象,将场景中Grid对象下的Tilemap子对象拖到GroundTilemap的赋值框中。
  • 修改PlaceGrid()方法的调用代码,用实例对象替代类名:
    // 替换原Tilemap.SetTile(...)代码
    GroundTilemap.SetTile(new Vector3Int(x, y, 0), Stone);
    

修改后的完整代码(关键部分已标注)

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;
    // 新增:Tilemap实例引用
    public Tilemap GroundTilemap;

    private int CurrentMapLength = 0;

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

    void Start()
    {
        PlaceGrid();
    }

    public void SewerGeneration(int value)
    {
        // 你的生成逻辑代码
    }

    //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
                    {
                        // 修改:用实例对象调用SetTile
                        GroundTilemap.SetTile(new Vector3Int(x, y, 0), Stone);
                    }
                }catch (System.Exception e)
                {

                }
            }
        }
    }
}

内容的提问来源于stack exchange,提问作者Energia360

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 15:45:56