当前上下文中不存在名称“实例化”

Est*_*ban 3 c# unity-game-engine

我正在学习 Unity C#,我正在做 Unity 的教程,2d roguelike,我们正在尝试实例化地砖,我所做的与视频中完全相同(实际上我什至复制了代码),但它显示了一个错误在线上

GameObject instance = Instantiate(toInstantiate, new Vector3(x, y, 0f), 
    Quaternion.identity) as GameObject; 
Run Code Online (Sandbox Code Playgroud)

特别是与Instantiate(toInstantiate).

你能帮助我吗?

using UnityEngine;
using System;
using System.Collections.Generic;
using Random = UnityEngine.Random;

public class BoardManager : MonoBehaviour 
{
    // Other class code omitted

    void BoardSetup()
    {
        boardHolder = new GameObject("Board").transform;

        for (int x = -1; x < columns + 1; x++)
        {
            for (int y = -1; y < rows + 1; y++)
            {
                GameObject toInstantiate = floorTiles[Random.Range(0, floorTiles.Length)];

                if (x == -1 || x == columns || y == -1 || y == rows)
                    toInstantiate = outerWallTiles[Random.Range(0, outerWallTiles.Length)];

                GameObject instance = Instantiate(toInstantiate, new Vector3(x, y, 0f), 
                    Quaternion.identity) as GameObject;

                instance.transform.SetParent(boardHolder);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Dav*_*vid 6

Instantiate(通常)是对 的引用Object.Instantiate,因此您正在编码的类需要派生自 unity 的Object类;这通常是通过继承来完成的,而MonoBehaviour后者又继承自Object。请记住,仅通过名称引用的任何方法都需要存在于同一类或继承中。