我正在制作一个spawner脚本,但遇到一个我无法找到解决方案的问题...
我收到错误消息
"无法隐式转换类型' UnityEngine.Vector3'到UnityEngine.Transform'"
我可以通过向public GameObject[] spawningObj;
public GameObject[] insects;
public GameObject[] invierment;
public GameObject[] inviermentSingel;
public Transform player;
public int maxNPCsPerChunk;
public int maxInectsPerChunk;
public int maxInviermentalsPerChunk;
public int spawningAria;
private Transform spawningpos;
// Use this for initialization
void Start()
{
if (player == null)
player = GameObject.FindWithTag("Player").transform;
int max = Random.Range(0, maxInectsPerChunk);
for (int i = 0; i < max; i++)
{
spawningpos = new Vector3(Random.Range(-spawningAria, spawningAria) + transform.position.x, 2f, Random.Range(-spawningAria, spawningAria) + transform.position.z);
//Error on above line
Instantiate(insects[Random.Range(0, insects.Length)], spawningpos);
}
添加.position来删除错误,但这不会起作用,因为它不是对象的变换只是脚本中的变换变量
SELECT prod
FROM yourTable
GROUP BY prod
HAVING SUM(CASE WHEN catid = 1 THEN 1 ELSE 0 END) > 0 AND
SUM(CASE WHEN catid = 2 THEN 1 ELSE 0 END) > 0 AND
SUM(CASE WHEN catid = 3 THEN 1 ELSE 0 END) > 0 AND
SUM(CASE WHEN catid = 4 THEN 1 ELSE 0 END) > 0 AND
SUM(CASE WHEN catid = 5 THEN 1 ELSE 0 END) > 0
答案 0 :(得分:7)
您收到的错误很好地解释了您的问题,让我们一起来看看
&#34;无法隐式转换类型&#39; UnityEngine.Vector3&#39;到UnityEngine.Transform&#39;&#34;
那你在哪里尝试将Transform
的值设置为Vector3?
private Transform spawningpos;
spawningpos
是转换
spawningpos = new Vector3(Random.Range(-spawningAria, spawningAria) + transform.position.x, 2f, Random.Range(-spawningAria, spawningAria) + transform.position.z);
但是你明确将其值设置为Vector3
Instantiate(insects[Random.Range(0, insects.Length)], spawningpos);
然后在这一行上实例化对象 - 使用Transform
作为输入。正如您将找到here,您正在使用该方法的重载实际上是要求父转换,请尝试此操作
private Vector3 spawningpos;
跟随之前一样
spawningpos = new Vector3(Random.Range(-spawningAria, spawningAria) + transform.position.x, 2f, Random.Range(-spawningAria, spawningAria) + transform.position.z);
最后
Instantiate(insects[Random.Range(0, insects.Length)], spawningpos, Quaternion.identity);
此代码将调用Instantiate
方法的正确重载,在传递Vector3
时必须设置对象的旋转
答案 1 :(得分:0)
您不能直接将 Vector3 对象转换为 Transform。我找到了一个简单的方法。
试试我下面的演示代码,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CubeSpawner : MonoBehaviour {
[SerializeField] GameObject cube;
[SerializeField] Transform spawnTransform;
// Start is called before the first frame update
void Start() {
InvokeRepeating("SpawnCubes", 2, 0.5f);
}
// Update is called once per frame
void Update(){
}
void SpawnCubes() {
spawnTransform.position = new Vector3 (Random.Range (0f, 10f), 18f, 0f);
GameObject cube1 =Instantiate(cube, spawnTransform) as GameObject;
}
}