“函数没有重载”实例化“需要4个参数”

时间:2017-10-30 02:49:30

标签: c# unity3d unity5

您好我正在关注Quill18制作的Unity教程。在我的代码中我试图实例化一些十六进制预制件。

using UnityEngine;
using System.Collections;

public class HexMap : MonoBehaviour {

    // Use this for initialization
    void Start () {
        GenerateMap ();
    }
    public GameObject HexPrefab;
    public void GenerateMap()
    {
        for (int column = 0; column < 10; column++) {
            for (int row = 0; row < 10; row++) 
            {
                Instantiate (HexPrefab, new Vector3 (column, 0, row), Quaternion.identity, this.transform); //this is the exact code he used and was working for him
            }
        }
    }

}

实例化方法给了我麻烦。即使是在线文档说我可以传递4个参数,但我收到错误“没有函数重载”实例化“需要4个参数”。 脚本组件附加到Empty。

1 个答案:

答案 0 :(得分:2)

Unity3D's 5.3 documentation表明Object.Instantiate没有定义,需要四个参数。但是,starting from 5.4,您可以按预期使用Instantiate方法。确保您的Unity版本与教程相同。

更改Unity 5.3.x的Instantiated GameObject的父级的解决方法如下:

public void GenerateMap()
{
    GameObject GO;
    for (int column = 0; column < 10; column++) {
        for (int row = 0; row < 10; row++) 
        {
            GO = Instantiate (HexPrefab, new Vector3 (column, 0, row), Quaternion.identity) as GameObject; 
            GO.transform.parent = this.transform;
        }
    }
}