所以我正在使用C#进行统一游戏,我正在尝试制作克隆然后将其删除。所以我发布的代码重新生成了玩家,当他重生时,火花就会飞出来。这使得克隆了火花。我无法删除火花。我收到错误消息:
无法将类型unityengine.transform转换为unityengine.gameobject 经由.....
所以我需要知道我的代码有什么问题以及为什么这样做。
所以这是整个代码
using UnityEngine;
using System.Collections;
public class GameMaster : MonoBehaviour {
public static GameMaster gm;
void Start () {
if (gm == null) {
gm = GameObject.FindGameObjectWithTag ("GM").GetComponent<GameMaster>();
}
}
public Transform playerPrefab;
public Transform spawnPoint;
public float spawnDelay = 2;
public Transform spawnPrefab;
public IEnumerator RespawnPlayer () {
//audio.Play ();
yield return new WaitForSeconds (spawnDelay);
Instantiate (playerPrefab, spawnPoint.position, spawnPoint.rotation);
GameObject clone = Instantiate (spawnPrefab, spawnPoint.position, spawnPoint.rotation) as GameObject;
Destroy (clone, 3f);
}
public static void KillPlayer (Player player) {
Destroy (player.gameObject);
gm.StartCoroutine (gm.RespawnPlayer());
}
}
这就是它正在弄乱的线
GameObject clone = Instantiate (spawnPrefab, spawnPoint.position, spawnPoint.rotation) as GameObject;
答案 0 :(得分:3)
您收到错误是因为当您执行Transform
时,预制件被声明为public Transform spawnPrefab;
。因此,您将其实例化为Transform
而不是GameObject。
要解决此问题,只需更改
即可public Transform spawnPrefab;
到
public GameObject spawnPrefab;
答案 1 :(得分:1)
可以实例化为transform
,只需在你的销毁线中销毁gameObject
:
Transform clone = Instantiate(spawnPrefab, spawnPoint.position, spawnPoint.rotation) as Transform;
Destroy(clone.gameObject, 3f);