2016-12-13 63 views
0

因此,我正在使用C#編寫一個具有統一性的遊戲,我正在嘗試創建一個克隆然後將其刪除。所以我發佈的代碼重新生成了玩家,並且在他重生時有火花飛出。這使得火花克隆。我無法刪除火花。我得到的錯誤信息:無法將類型轉換爲類型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; 

回答

3

,因爲當你做了public Transform spawnPrefab;您的預製聲明​​爲Transform你得到錯誤的行。所以,你將它實例化爲Transform而不是GameObject。

要解決它,只需

public Transform spawnPrefab; 

public GameObject spawnPrefab; 
1

這是確定實例爲transform,就摧毀它的gameObject在你破壞行:

Transform clone = Instantiate(spawnPrefab, spawnPoint.position, spawnPoint.rotation) as Transform; 
Destroy(clone.gameObject, 3f); 
相關問題