[英]Cannot convert type to type c#
So I am working on a game with unity using C# and I'm trying to make a clone then delete it. 因此,我正在使用C#统一开发一个游戏,并且尝试制作一个克隆然后删除它。 So the code I posted respawns the player and has sparks fly out when he respawns.
因此,我发布的代码会重生玩家,并在重生时发出火花。 This makes a clone of the sparks.
这使火花克隆。 I am having trouble deleting the sparks.
我无法删除火花。 I get the error message:
我收到错误消息:
cannot convert type unityengine.transform to unityengine.gameobject via.....
无法通过.....将类型unityengine.transform转换为unityengine.gameobject
so I need to know what is wrong with my code and why it is doing this. 所以我需要知道我的代码出了什么问题以及为什么这样做。
so here is the whole code 所以这是整个代码
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());
}
}
and here is the line it is messing up on 这是它正在混乱的线
GameObject clone = Instantiate (spawnPrefab, spawnPoint.position, spawnPoint.rotation) as GameObject;
You get the error because your prefab is declared as a Transform
when you did public Transform spawnPrefab;
你得到的错误,因为你的预制被声明为一个
Transform
,当你做了public Transform spawnPrefab;
. 。 So, you are Instantiating it as a
Transform
instead of GameObject. 因此,您将其实例化为
Transform
而不是GameObject。
To fix it, simply change 要修复它,只需更改
public Transform spawnPrefab;
to 至
public GameObject spawnPrefab;
It is ok to instantiate as a transform
, just destroy it's gameObject
in your destroy line: 可以将其实例化为
transform
,只需在销毁行中销毁它的gameObject
:
Transform clone = Instantiate(spawnPrefab, spawnPoint.position, spawnPoint.rotation) as Transform;
Destroy(clone.gameObject, 3f);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.