我想做的就是编写一个像flappy bird这样的自动生成游戏,但我遇到了从未听说过的错误,据我所研究,我认为其他人都没有经历过这些错误参数3:无法从"(int,int,int("转换为"四元数"参数4:无法转换"UnityEngine"。四元数"到"UnityEngine。Transfrom'
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawner : MonoBehaviour
{
public GameObject prefab;
public int objectCount;
public float spacing;
public GameObject prefab2;
public int Height;
void Start()
{
var position = new Vector3();
for (int i = 0; i < objectCount; i++)
{
Instantiate(prefab, position, (0, Random.Range(-Height, Height), 0), Quaternion.identity);
position.x += spacing;
}
}
}
我已经试过把它变成一个浮子了,但我没有工作,所以我是不是错过了什么?
没有
Instantiate(GameObject, Vector3, (int,int,int), Quaternion)
我想您想将随机高度添加到位置,而不是将其设置为第三个参数。尝试
Instantiate(prefab, position + Vector3.up * Random.Range(-Height,Height), Quaternion.identity);
相反。
此外,如果您需要创建Vector3;(x,y,z(";不够,您必须键入";新矢量3(x,y,z(";。
您正在使用的实例化声明需要4个参数,因此它必须与此匹配:
public static Object Instantiate(Object original, Vector3 position, Quaternion rotation, Transform parent);
最后两个参数是四元数旋转&变换父对象。看起来您需要将"四元数"identity作为倒数第二个参数,并将"父变换"设置为最后一个参数。
Instantiate(prefab, position, Quaternion.identity, PARENT);
您正在用4个参数调用Instantiate
,其中参数为
Instantiate(GameObjectToInstantiate, PositionInVector3, RotationInQuaternion, transform);
出于这个原因,unity在第三个参数中期望Quaternion
,但您正在通过(0, Random.Range(-Height, Height), 0)